Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
import { ObjectModel, BasicModel } from "objectmodel";
let N = BasicModel(Number).defaultTo(1);
console.log(N(5) + N());
const FileInfo = ObjectModel({
name: String,
size: [Number],
creationDate: [Date],
writable: Boolean
}).defaultTo({
name: "Untitled file",
size: 0,
writable: true
});
let file = new FileInfo({ writable: false });
console.log(file.name); // name is mandatory but a default value was passed
export const Integer = BasicModel(Number).assert(Number.isInteger).as("Integer");
export const SafeInteger = BasicModel(Number).assert(Number.isSafeInteger).as("SafeInteger");
export const FiniteNumber = BasicModel(Number).assert(Number.isFinite).as("FiniteNumber");
export const PositiveNumber = BasicModel(Number).assert(function isPositive(n) { return n >= 0 }).as("PositiveNumber");
export const NegativeNumber = BasicModel(Number).assert(function isNegative(n) { return n <= 0 }).as("NegativeNumber");
export const PositiveInteger = PositiveNumber.extend().assert(Number.isInteger).as("PositiveInteger");
export const NegativeInteger = NegativeNumber.extend().assert(Number.isInteger).as("NegativeInteger");
// Strings
export const StringNotBlank = BasicModel(String).assert(function isNotBlank(str) { return str.trim().length > 0 }).as("StringNotBlank");
export const NormalizedString = BasicModel(String).assert(function isNormalized(str) { return str.normalize() === str }).as("NormalizedString");
export const TrimmedString = BasicModel(String).assert(function isTrimmed(str) { return str.trim() === str }).as("TrimmedString");
// Dates
export const PastDate = BasicModel(Date).assert(function isInThePast(date) { return date.getTime() < Date.now() }).as("PastDate");
export const FutureDate = BasicModel(Date).assert(function isInTheFuture(date) { return date.getTime() > Date.now() }).as("FutureDate");
// Arrays
export const ArrayNotEmpty = BasicModel(Array).assert(function isNotEmpty(arr) { return arr.length > 0 }).as("ArrayNotEmpty");
export const ArrayUnique = BasicModel(Array).assert(function hasNoDuplicates(arr) { return arr.every((x, i) => arr.indexOf(x) === i) }).as("ArrayUnique");
export const ArrayDense = BasicModel(Array).assert(function hasNoHoles(arr) { return arr.filter(() => true).length === arr.length }).as("ArrayDense");
// Others
export const PromiseOf = model => p => BasicModel(Promise)(p).then(x => model(x));
// Examples of commonly used models
import { BasicModel } from "objectmodel"
export const Primitive = BasicModel([Boolean, Number, String, Symbol]).as("Primitive");
// Booleans-like
export const Falsy = BasicModel([Primitive, null, undefined]).assert(function isFalsy(x) { return !x }).as("Falsy");
export const Truthy = BasicModel([Primitive, Object]).assert(function isTruthy(x) { return !!x }).as("Truthy");
// Numbers
export const Integer = BasicModel(Number).assert(Number.isInteger).as("Integer");
export const SafeInteger = BasicModel(Number).assert(Number.isSafeInteger).as("SafeInteger");
export const FiniteNumber = BasicModel(Number).assert(Number.isFinite).as("FiniteNumber");
export const PositiveNumber = BasicModel(Number).assert(function isPositive(n) { return n >= 0 }).as("PositiveNumber");
export const NegativeNumber = BasicModel(Number).assert(function isNegative(n) { return n <= 0 }).as("NegativeNumber");
export const PositiveInteger = PositiveNumber.extend().assert(Number.isInteger).as("PositiveInteger");
export const NegativeInteger = NegativeNumber.extend().assert(Number.isInteger).as("NegativeInteger");
// Strings
export const StringNotBlank = BasicModel(String).assert(function isNotBlank(str) { return str.trim().length > 0 }).as("StringNotBlank");
// Examples of commonly used models
import { BasicModel } from "objectmodel"
export const Primitive = BasicModel([Boolean, Number, String, Symbol]).as("Primitive");
// Booleans-like
export const Falsy = BasicModel([Primitive, null, undefined]).assert(function isFalsy(x) { return !x }).as("Falsy");
export const Truthy = BasicModel([Primitive, Object]).assert(function isTruthy(x) { return !!x }).as("Truthy");
// Numbers
export const Integer = BasicModel(Number).assert(Number.isInteger).as("Integer");
export const SafeInteger = BasicModel(Number).assert(Number.isSafeInteger).as("SafeInteger");
export const FiniteNumber = BasicModel(Number).assert(Number.isFinite).as("FiniteNumber");
export const PositiveNumber = BasicModel(Number).assert(function isPositive(n) { return n >= 0 }).as("PositiveNumber");
export const NegativeNumber = BasicModel(Number).assert(function isNegative(n) { return n <= 0 }).as("NegativeNumber");
export const PositiveInteger = PositiveNumber.extend().assert(Number.isInteger).as("PositiveInteger");
export const NegativeInteger = NegativeNumber.extend().assert(Number.isInteger).as("NegativeInteger");
// Strings
export const StringNotBlank = BasicModel(String).assert(function isNotBlank(str) { return str.trim().length > 0 }).as("StringNotBlank");
export const NormalizedString = BasicModel(String).assert(function isNormalized(str) { return str.normalize() === str }).as("NormalizedString");
export const TrimmedString = BasicModel(String).assert(function isTrimmed(str) { return str.trim() === str }).as("TrimmedString");
// Dates
export const PastDate = BasicModel(Date).assert(function isInThePast(date) { return date.getTime() < Date.now() }).as("PastDate");
export const FutureDate = BasicModel(Date).assert(function isInTheFuture(date) { return date.getTime() > Date.now() }).as("FutureDate");
// Arrays
export const ArrayNotEmpty = BasicModel(Array).assert(function isNotEmpty(arr) { return arr.length > 0 }).as("ArrayNotEmpty");
export const ArrayUnique = BasicModel(Array).assert(function hasNoDuplicates(arr) { return arr.every((x, i) => arr.indexOf(x) === i) }).as("ArrayUnique");
export const ArrayDense = BasicModel(Array).assert(function hasNoHoles(arr) { return arr.filter(() => true).length === arr.length }).as("ArrayDense");
// Examples of commonly used models
import { BasicModel } from "objectmodel"
export const Primitive = BasicModel([Boolean, Number, String, Symbol]).as("Primitive");
// Booleans-like
export const Falsy = BasicModel([Primitive, null, undefined]).assert(function isFalsy(x) { return !x }).as("Falsy");
export const Truthy = BasicModel([Primitive, Object]).assert(function isTruthy(x) { return !!x }).as("Truthy");
// Numbers
export const Integer = BasicModel(Number).assert(Number.isInteger).as("Integer");
export const SafeInteger = BasicModel(Number).assert(Number.isSafeInteger).as("SafeInteger");
export const FiniteNumber = BasicModel(Number).assert(Number.isFinite).as("FiniteNumber");
export const PositiveNumber = BasicModel(Number).assert(function isPositive(n) { return n >= 0 }).as("PositiveNumber");
export const NegativeNumber = BasicModel(Number).assert(function isNegative(n) { return n <= 0 }).as("NegativeNumber");
export const PositiveInteger = PositiveNumber.extend().assert(Number.isInteger).as("PositiveInteger");
export const NegativeInteger = NegativeNumber.extend().assert(Number.isInteger).as("NegativeInteger");
// Strings
export const StringNotBlank = BasicModel(String).assert(function isNotBlank(str) { return str.trim().length > 0 }).as("StringNotBlank");
export const NormalizedString = BasicModel(String).assert(function isNormalized(str) { return str.normalize() === str }).as("NormalizedString");
export const TrimmedString = BasicModel(String).assert(function isTrimmed(str) { return str.trim() === str }).as("TrimmedString");
// Dates
import { BasicModel } from "objectmodel";
const PositiveInteger = BasicModel(Number)
.assert(Number.isInteger)
.assert(n => n >= 0, "should be greater or equal to zero");
function isPrime(n) {
for (let i = 2, m = Math.sqrt(n); i <= m; i++) {
if (n % i === 0) return false;
}
return n > 1;
}
const PrimeNumber = PositiveInteger.extend().assert(isPrime);
//// extend to not add isPrime assertion to the Integer model
PositiveInteger(-1);
// TypeError: assertion should be greater or equal to zero returned false for value - 1
export const PositiveInteger = PositiveNumber.extend().assert(Number.isInteger).as("PositiveInteger");
export const NegativeInteger = NegativeNumber.extend().assert(Number.isInteger).as("NegativeInteger");
// Strings
export const StringNotBlank = BasicModel(String).assert(function isNotBlank(str) { return str.trim().length > 0 }).as("StringNotBlank");
export const NormalizedString = BasicModel(String).assert(function isNormalized(str) { return str.normalize() === str }).as("NormalizedString");
export const TrimmedString = BasicModel(String).assert(function isTrimmed(str) { return str.trim() === str }).as("TrimmedString");
// Dates
export const PastDate = BasicModel(Date).assert(function isInThePast(date) { return date.getTime() < Date.now() }).as("PastDate");
export const FutureDate = BasicModel(Date).assert(function isInTheFuture(date) { return date.getTime() > Date.now() }).as("FutureDate");
// Arrays
export const ArrayNotEmpty = BasicModel(Array).assert(function isNotEmpty(arr) { return arr.length > 0 }).as("ArrayNotEmpty");
export const ArrayUnique = BasicModel(Array).assert(function hasNoDuplicates(arr) { return arr.every((x, i) => arr.indexOf(x) === i) }).as("ArrayUnique");
export const ArrayDense = BasicModel(Array).assert(function hasNoHoles(arr) { return arr.filter(() => true).length === arr.length }).as("ArrayDense");
// Others
export const PromiseOf = model => p => BasicModel(Promise)(p).then(x => model(x));
// Booleans-like
export const Falsy = BasicModel([Primitive, null, undefined]).assert(function isFalsy(x) { return !x }).as("Falsy");
export const Truthy = BasicModel([Primitive, Object]).assert(function isTruthy(x) { return !!x }).as("Truthy");
// Numbers
export const Integer = BasicModel(Number).assert(Number.isInteger).as("Integer");
export const SafeInteger = BasicModel(Number).assert(Number.isSafeInteger).as("SafeInteger");
export const FiniteNumber = BasicModel(Number).assert(Number.isFinite).as("FiniteNumber");
export const PositiveNumber = BasicModel(Number).assert(function isPositive(n) { return n >= 0 }).as("PositiveNumber");
export const NegativeNumber = BasicModel(Number).assert(function isNegative(n) { return n <= 0 }).as("NegativeNumber");
export const PositiveInteger = PositiveNumber.extend().assert(Number.isInteger).as("PositiveInteger");
export const NegativeInteger = NegativeNumber.extend().assert(Number.isInteger).as("NegativeInteger");
// Strings
export const StringNotBlank = BasicModel(String).assert(function isNotBlank(str) { return str.trim().length > 0 }).as("StringNotBlank");
export const NormalizedString = BasicModel(String).assert(function isNormalized(str) { return str.normalize() === str }).as("NormalizedString");
export const TrimmedString = BasicModel(String).assert(function isTrimmed(str) { return str.trim() === str }).as("TrimmedString");
// Dates
export const PastDate = BasicModel(Date).assert(function isInThePast(date) { return date.getTime() < Date.now() }).as("PastDate");
export const FutureDate = BasicModel(Date).assert(function isInTheFuture(date) { return date.getTime() > Date.now() }).as("FutureDate");
// Arrays
export const ArrayNotEmpty = BasicModel(Array).assert(function isNotEmpty(arr) { return arr.length > 0 }).as("ArrayNotEmpty");
export const ArrayUnique = BasicModel(Array).assert(function hasNoDuplicates(arr) { return arr.every((x, i) => arr.indexOf(x) === i) }).as("ArrayUnique");
export const ArrayDense = BasicModel(Array).assert(function hasNoHoles(arr) { return arr.filter(() => true).length === arr.length }).as("ArrayDense");
// Others
export const PromiseOf = model => p => BasicModel(Promise)(p).then(x => model(x));
// Examples of commonly used models
import { BasicModel } from "objectmodel"
export const Primitive = BasicModel([Boolean, Number, String, Symbol]).as("Primitive");
// Booleans-like
export const Falsy = BasicModel([Primitive, null, undefined]).assert(function isFalsy(x) { return !x }).as("Falsy");
export const Truthy = BasicModel([Primitive, Object]).assert(function isTruthy(x) { return !!x }).as("Truthy");
// Numbers
export const Integer = BasicModel(Number).assert(Number.isInteger).as("Integer");
export const SafeInteger = BasicModel(Number).assert(Number.isSafeInteger).as("SafeInteger");
export const FiniteNumber = BasicModel(Number).assert(Number.isFinite).as("FiniteNumber");
export const PositiveNumber = BasicModel(Number).assert(function isPositive(n) { return n >= 0 }).as("PositiveNumber");
export const NegativeNumber = BasicModel(Number).assert(function isNegative(n) { return n <= 0 }).as("NegativeNumber");
export const PositiveInteger = PositiveNumber.extend().assert(Number.isInteger).as("PositiveInteger");
export const NegativeInteger = NegativeNumber.extend().assert(Number.isInteger).as("NegativeInteger");
// Strings
export const StringNotBlank = BasicModel(String).assert(function isNotBlank(str) { return str.trim().length > 0 }).as("StringNotBlank");
export const NormalizedString = BasicModel(String).assert(function isNormalized(str) { return str.normalize() === str }).as("NormalizedString");
export const TrimmedString = BasicModel(String).assert(function isTrimmed(str) { return str.trim() === str }).as("TrimmedString");
// Dates
export const PastDate = BasicModel(Date).assert(function isInThePast(date) { return date.getTime() < Date.now() }).as("PastDate");
export const FutureDate = BasicModel(Date).assert(function isInTheFuture(date) { return date.getTime() > Date.now() }).as("FutureDate");
// Arrays
export const PromiseOf = model => p => BasicModel(Promise)(p).then(x => model(x));