parzhitsky / check Goto Github PK
View Code? Open in Web Editor NEWYet another value-validator for JavaScript ... Or is it?
License: MIT License
Yet another value-validator for JavaScript ... Or is it?
License: MIT License
check.nand(false, false); // true
check.nand(false, true); // true
check.nand(true, false); // true
check.nand(true, true); // false
function func() { /* ... */ }
check.isFunction(func); // true
check.isFunction(() => {}); // true
check.isFunction({}); // false
check.isNotFunction(func); // false
check.isNotFunction(() => {}); // false
check.isNotFunction({}); // true
check.and(false, false); // false
check.and(false, true); // false
check.and(true, false); // false
check.and(true, true); // true
// incorrect usage
check.isEqualTo({ prop: 1 }, { prop: 1 }); // false
// correct usage
check.equals({ prop: 1 }, { prop: 1 }); // true
check.equals({ prop: 1 }, { prop: 2 }); // false
check.isGenerator(function*(){}); // true
check.isGenerator(function(){}); // false
check.isGenerator(42); // false
check.isNotGenerator(function*(){}); // false
check.isNotGenerator(function(){}); // true
check.isNotGenerator(42); // true
Alongside with using check
as a node module…
npm install checkjs --save
… allow installation of it though the <script>
tag in the <html>
page
<script src="./lib/check.js"></script>
let reverseRange = [5, -1];
check(3).isInRange(reverseRange); // `false`; should be `true`
cheek.bundle(methodNames, inputs);
cheek.everyMethod(methodNames, inputs);
cheek.someMethod(methodNames, inputs);
English is not my native language (and obviously will never be). So feel free to spot here in the comments any mistake that you can see in readme docs or commit names or issues etc.
Some parts of the code use silly protections that can be useful only in old-standard environments:
if (typeof Symbol !=== "undefined") && input[Symbol.iterator]
while others may cause fatal SyntaxError
in these environments:
(...args) => check[methodName](...args)
So, should the check
be based on old or new standards?
Since January there is a native Array.prototype.includes
method in JavaScript.
To avoid confusions, the check.includes
method will now work with arrays as long as strings.
let array = [];
check.isCircular(array); // false
array.push(array);
check.isCircular(array); // true
check.someMethod()
check.someInput()
let obj = { foo: { bar: "baz" } };
check.isDefined(obj.prop); // false
check.isDefined(obj.prop.bar); // TypeError!
check.isDefined(obj.prop.bar, "safe"); // false
check.or(false, false); // false
check.or(false, true); // true
check.or(true, false); // true
check.or(true, true); // true
let object = {
prop1: { foo: { bar: 5 } },
prop2: { foo: { bar: undefined } },
prop3: 42
};
// alias: check.prop()
check.hasProperty(object, "prop1"); // true
check.hasProperty(object, "prop5"); // false
check.hasProperty(object, "bar"); // false
check.hasProperty(object, ["prop1", "bar"]); // false
check.hasProperty(object, ["prop1", "foo", "bar"]); // true
check.hasProperty(object, ["prop2", "foo", "bar"]); // true (!)
check.hasProperty(object, ["prop3", "foo", "bar"]); // false
// alias: check.noprop()
check.hasNoProperty(object, "prop1"); // false
check.hasNoProperty(object, "prop5"); // true
check.hasNoProperty(object, "bar"); // true
check.hasNoProperty(object, ["prop1", "bar"]); // true
check.hasNoProperty(object, ["prop1", "foo", "bar"]); // false
check.hasNoProperty(object, ["prop2", "foo", "bar"]); // false (!)
check.hasNoProperty(object, ["prop3", "foo", "bar"]); // true
check.isCallable(function() {}); // true
check.isCallable(() => {}); // true
check.isCallable({}); // false
check.isNotCallable(function() {}); // false
check.isNotCallable(() => {}); // false
check.isNotCallable({}); // true
Since March 11th the Proxy chapter serves the same purpose much better for any number of inputs and a single method. Multiple methods, however, require issue #10 to be solved.
to the chapter Types:
cheek.isArraylike(input)
to the chapter Array:
cheek.arrayContains(array: any[], element: any)
cheek.hasFirst(array: any[], element: any)
cheek.hasLast(array: any[], element: any)
to the chapter String:
cheek.isEmptyString(input: string)
cheek.stringContains(source: string, substr: string)
cheek.startsWith(source: string, substr: string)
cheek.endsWith(source: string, substr: string)
to the chapter Number:
cheek.isZero(input: number)
to the chapter Other:
cheek.inputs(inputs: any[])
(analogue of cheek.bundle()
)cheek.every(inputs: any[])
(analogue of cheek.everyInput()
)cheek.some(inputs: any[])
(analogue of cheek.someInput()
)cheek.input(42).isPrimitive().isNumber().isInRange([40, 50], "inclusively"); // true
At least two options are possible:
check.input(42).chain(logic);
check.input(42)[logic]();
where logic
may take one of the following string-values:
"and"
- conjuction;"or"
- disjunction;"nor"
- negated disjunction;"xor"
- exclusive disjunction;Both options may be used along one with another as alias to each other.
The final result of the chain should always be boolean, no matter how long the chain is. But how to retrieve the result when needed?
One option is to use function .resolve()
(or some short alias like .so()
) at the end of the chain. It is easy to setup but not so convenient to use. There should be a better way.
Another option is to create new class extends Boolean {}
and alter it to our needs. Gotta check this out.
check.xor(false, false); // false
check.xor(false, true); // true
check.xor(true, false); // true
check.xor(true, true); // false
check.def = check.isDefined;
check.ndef = check.isNotDefined;
check.nor(false, false); // true
check.nor(false, true); // false
check.nor(true, false); // false
check.nor(true, true); // false
This method is intuitively expected to work as one that returns an array of boolean values
A declarative, efficient, and flexible JavaScript library for building user interfaces.
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
An Open Source Machine Learning Framework for Everyone
The Web framework for perfectionists with deadlines.
A PHP framework for web artisans
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
Some thing interesting about web. New door for the world.
A server is a program made to process requests and deliver data to clients.
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
Some thing interesting about visualization, use data art
Some thing interesting about game, make everyone happy.
We are working to build community through open source technology. NB: members must have two-factor auth.
Open source projects and samples from Microsoft.
Google ❤️ Open Source for everyone.
Alibaba Open Source for everyone
Data-Driven Documents codes.
China tencent open source team.