Gathering detailed insights and metrics for checker-as-is
Gathering detailed insights and metrics for checker-as-is
Gathering detailed insights and metrics for checker-as-is
Gathering detailed insights and metrics for checker-as-is
@pro-script/as-is
Check your types at runtime with ESNext syntax by meta programing in node.js and browser with interfaces, types, strict object and more.
whois-checker
This is domain registration checker with the whois-parsed as dependenci
nodehun
The Hunspell binding for nodejs that exposes as much of hunspell as possible and also adds new features. Hunspell is a first class spellcheck library used by Google, Apple, and Mozilla.
grunt-port-checker
Verify that a port is available and update grunt configurations as necessary.
Check your types at runtime with ESNext syntax by meta programing in node.js and browser and even more.
npm install checker-as-is
Typescript
Module System
Min. Node Version
Node Version
NPM Version
JavaScript (99.25%)
HTML (0.75%)
Total Downloads
5,885
Last Day
2
Last Week
11
Last Month
95
Last Year
1,191
MIT License
3 Stars
38 Commits
1 Watchers
1 Branches
1 Contributors
Updated on Dec 15, 2024
Minified
Minified + Gzipped
Latest Version
0.9.32
Package Id
checker-as-is@0.9.32
Unpacked Size
230.26 kB
Size
58.89 kB
File Count
20
NPM Version
8.19.2
Node Version
18.11.0
Cumulative downloads
Total Downloads
Last Day
0%
2
Compared to previous day
Last Week
-52.2%
11
Compared to previous week
Last Month
-37.1%
95
Compared to previous month
Last Year
8.7%
1,191
Compared to previous year
Check your types at runtime with ESNext syntax by meta programing in node.js and browser with interfaces, strict object, enum type and more. Follow me on twitter for further updates twitter
This library respects the principle of code readability. The code should tell a story.
I.want.to.tell.you.a.story(myStory)
In traditional way we use functions to do the call
1as(type, value) 2// or 3is(type, value);
But that means you have to use a quoted string when you need to make a call
1is('string', stringVariable);
In the ECMAscript specification, object.property is always a string like this object['property']. So why can't we use this great standard feature to reduce quotas? We can use this feature and make our calls without quotas like as.string(value). But for this functionality, we need to implement specific methods in an object or class.
1const is = { 2 string: (value)=> { 3 return typeof value === 'string' 4 }, 5 number: ..., 6 boolean:..., 7 ..., 8 etc 9}
And it will be a lot of methods with almost the same functionality that we will need to refactor so as not to violate the DRY rule. Or we can use ESNext proxies and metaprogramming to do it in a simple way with lots of cool features inside. Like this
1as.string(value) ; 2 3//or 4 5is.string(value);
But remember that this is the same function call is(type, value) and you can always create an alias if you need to use it like in the old days.
1function is(type, value) { 2 return is[type](value) 3}; 4 5function as(type, value) { 6 return as[type](value) 7}; 8// and after that to use it like a function call 9is('number', 33) // instead of is.number(33)
So, if the property name in the is/as calls is a string variable, you can do cool things. If you implement two methods for checking strings and numbers as an example, you can do it like this.
1is.string_or_number(variable); 2// inside the "is" method 3...(type, varibale) { 4 if('string_or_number'.includes('string')) { 5 stringChecking(value); 6 }; 7 8 if(type.includes('number')) { 9 numberChecking(value); 10 } 11}
That's huge flexibility. You can write any text that includes "string" or "number" to make your code more readable.
1 2is.doctor_prescription_a_string_variable(prescription); // this is a working example 3const sum_first_plus_second = as.first_number(2) + as.second_number(3); // this is a working example 4// sum_first_plus_second -> 5
And, of course, the is.doctor_prescription a_string method is not implemented, because doctor_prescription_a_string_variable is just an unquoted string variable.
Types list with alias name:
Multi type checking:
IF/ELSE/END for type checking
IF.number(string_)? (
console.log('IF type checking')
):ELSE.string(string_)? (
console.log('ELSE type checking'),
expect(string_).to.be.eq(string_)
):END;
Strict type object:
strict.string`name`
strict.name = 'string'
// or
strict.string`name`.name = 'string'
Validators list:
Class checking:
is.date(Date);
is.date(new Date);
is.class(Date) or is.class(new Date)
Interface
1const { IUser } = Interface({
2 IUser: {
3 name: as.string,
4 age: as.number,
5 birthDate: as.date
6 }
7});
8IUser.pages = as.strings;
9delete IUser.birthDate;
10
11function example(name, age, _ = as.IUser = { name, age }) {
12 console.log(name, age);
13 return 'returned string';
14 }
15
16as.StringNumber(example({ name: 'text', age: 12, pages:['page'] }));
Integrations:
import {default as email} from 'validator/lib/isEmail';
is.email('foo@bar.com'); // true | false
as.email('foo@bar.com'); // foo@bar.com | TypeError
Utility
get.type(Promise); // Promise
get.type(43); // Number
Node.js
1npm i checker-as-is -S
Browsers
1<script type="module" src="https://unpkg.com/checker-as-is@latest/src/as-is.browser.min.js"></script>
Checker-as-is is a stateful module please keep this in mind.
1is['js type here']('argument here') // true | false 2as['js type here']('argument here') // argument | TypeError 3optional['js type here' + 'Undefined' + 'Null']('argument here') // argument | TypeError
An example
1import { Checker, BaseInterface, Enum, JSON5 } from 'checker-as-is'; 2const { multi, strict, as, is, optional } = new Checker(); 3 4 5//positive 6is.string('example string'); // true 7as.string('example string'); // example string 8optional.string() // undefined 9optional.string('example string') // example string 10 11//negative 12is.number('example string'); // false 13as.number('example string'); // TypeError: String is not a(an) number 14optional.number('example string'); // TypeError: String is not a(an) number
1import { Checker, BaseInterface, Enum, JSON5 } from 'checker-as-is'; 2const { multi, strict, as, is, optional } = new Checker(); 3 4function example(arg, arg2, arg3) { 5 as.string(arg); 6 as.number(arg2); 7 as.boolean(arg3); 8 9 console.log(arg, arg2, arg3); 10}; 11example(text, 2, true) 12// text, 2, true 13 14let result, one = 1, two = 2; 15as.number( 16 result = as.number(one) + as.number(two) 17) 18console.log(result); 19// 3 20
or next syntax
1import { Checker, BaseInterface, Enum, JSON5 } from 'checker-as-is'; 2const { multi, strict, as, is, optional } = new Checker(); 3 4function example(arg, arg2, arg3) { 5 as.string(arg), as.number(arg2), as.boolean(arg3); 6 7 console.log(arg, arg2, arg3); 8}; 9example(text, 2, true) 10//[ text, 2, true ]
or more extraordinary syntax
1import { Checker, BaseInterface, Enum, JSON5 } from 'checker-as-is'; 2const { multi, strict, as, is, optional } = new Checker(); 3 4function example(arg, arg2, arg3, 5 _ = [as.string(arg), as.number(arg2), as.boolean(arg3)]) { 6 console.log(type); 7}; 8example(text, 2, true) 9//[ text, 2, true ] 10
1import { Checker, BaseInterface, Enum, JSON5 } from 'checker-as-is'; 2const instance = new Checker(); 3 4is.Checker(Checker); // true 5as.Checker(Checker);// class Checker 6 7is.Checker(instance); // true 8as.Checker(instance);// class instance 9 10is.class(instance); // true 11as.class(instance);// class instance 12 13
In object, array, set and map. All types ending with 's' will be checked.
1is.strings(exampleObject) && as.strings(exampleObject);
2is.Numbers(exampleArray) && as.Numbers(exampleArray);
3is.Errors(exampleSet) && as.Errors(exampleSet);
4is.BigInts(exampleMap) && as.BigInts(exampleMap);
5is.BigInts(exampleMap) && optional.BigInts(exampleMap);
1strict['js type here']`variable name`;// <-- meta programing magic
1strict.string`name`; 2strict.name = 'Stephen Hawking';
1import { Checker, BaseInterface, Enum, JSON5 } from 'checker-as-is'; 2const { multi, strict, as, is } = new Checker(); 3strict.string`name`.name = 'Mike'; 4strict.number`age`; 5strict.strings`pages`; 6 7const $ = strict; // you can use alias for strict object 8 9$.age = 12; 10$.pages = ['pageOne', 'pageTwo']; 11$.bla = 1; // will not be assigned 12 13let { name, age, pages } = strict; // after that name, age and pages loose their strict behavior 14 15name = 2022 16console.log(name, age, pages ); 17// 2022 12 [ 'pageOne', 'pageTwo' ] 18 19$.name = 2022; // but strict.name still has it 20// TypeError: Number is not a(an) string
Strict has reserved variable names: get, set, variable, lastType. This means that you can't do the following;
1strict.null`get`; 2//or 3strict.undefined`set`; 4//or 5strict.object`variables`;
When a variable is part of more than one type, you can also check for that.
1is['couple js type here']('argument here'); // true | false 2as['couple js type here']('argument here'); // argument | TypeError 3 4// as alias syntax 5multi`couple js type here`('argument here'); // argument | TypeError 6multi(['couple js type here'])('argument here'); // argument | TypeError 7
1as.NumberStringBoolean(2022); 2as.Number_String_Boolean_Symbol_Function_BigInt_Array([]); 3as['Number|String|Boolean']('text'); 4as['Number-String-Boolean'](true); 5as['Number or String or Boolean'](777); 6as['it can be any text with type definition like String or Number'](111); 7 8multi`Number|String|Boolean`(2022); 9multi(['Number|String|Boolean'])(true); 10 11const multiType = 'Number|String|Boolean'; 12as[multiType]({}); 13// TypeError: Object is not a(an) Number|String|Boolean
First you need create an interface, which will be stored in instance of checker in private area #interfaces.
1const checker = new Checker(); 2const { multi, Interface, strict, as, is } = checker; 3 4const { [InterfaceName] } = Interface({ 5 [InterfaceName]: { 6 [propertyName]: ['as[js type here]'] 7 } 8}); 9// as result => checker.#interfaces[InterfaceName]
Working example
1const { Interface, as } = new Checker(); 2 3const { IUser } = Interface({ 4 IUser: { 5 name: as.string 6 } 7});
When the interface is ready, you can change it.
1IUser.pages = as.strings;
2IUser.birthDate = as.number;
3delete IUser.birthDate;
The method Interface receives an object of objects, where the properties are a reference to Checker-as-is type checking methods. You can use BaseInterface to create an interface object after instantiation. This gives you the ability to work with interfaces like classes.
1// MyInterface.interface.js 2 3export default class MyInterface extends BaseInterface { 4 5 age = ()=> as.number; 6 7 constructor() { 8 super(MyInterface); 9 } 10 11 name() { 12 return as.string 13 } 14 15 static surName() { 16 return as.string 17 } 18}
After that
1import MyInterface from './MyInterface.interface.js'; 2 3const { IMyInterface } = Interface({ IMyInterface: new MyInterface }); 4as.IMyInterface = { name: 'Tomas', age: 33, surName: 'Andersen' };
1const { IUser, IBook } = Interface({
2 IUser: {
3 name: as.string,
4 age: as.number,
5 birthDate: as.date
6 },
7 IBook: {
8 title: as.string,
9 pages: as.number
10 }
11});
12
13IUser.pages = as.strings;
14delete IUser.birthDate;
15
16as.IUser = { name: 'text', age: 12, pages:['page'] };
17
18function example(params, Interface = (as.IUser = params)) {
19 return 'returned string';
20}
21
22function exampleSecond(params) {
23 const { title, pages } = as.IBook = params;
24 return params
25}
26
27
28// to check returned interface use "set"
29as.IBook = example({ name: 'text', age: 12, pages:['page'] });
30// to check returned value use "call"
31as.string(exampleSecond({ title: 'Book title', pages: 777}));
You can to get all interfaces from Checker instance like this:
1const intefaces = Interface({}); 2// => { IUser, IBook, IMyInterface }
These commands are an alias for the "is" command and are added to make the code easier to read.
When you need to use a couple of variants of function or method calls, you can do the following
1// Case 1 2function someFunction(name, age, friends) { 3 console.log(name, age, friends); 4} 5// Case 2 6function someFunction(name, friends) { 7 console.log(name, friends); 8} 9// Case 3 10function someFunction(age, friends) { 11 console.log(age, friends); 12} 13// Case 4 14function someFunction(friends) { 15 console.log(friends); 16} 17 18function someFunction(name, age, friends, 19 _= [as.stringNumberArray(name), 20 as.undefinedNumberArray(age), 21 as.undefinedArray(friends) 22 ]) { 23 24 IF.string(name) && is.number(age) && is.array(friends)? ( 25 as.array(_) && as.notEmpty(_) 26 ):ELSE.string(name) && is.array(age)? ( 27 friends = age, 28 age = undefined 29 ):ELSE.number(name) && is.array(age)? ( 30 friends = age, 31 age = name, 32 name = undefined 33 ):ELSE.array(name)? ( 34 friends = name, 35 name = undefined 36 ):END; 37 console.log(`name: ${name}, age: ${age}, friends: ${friends}`); 38} 39someFunction('Rick', 25, ['Mike', 'Liza']); 40// name: Rick, age: 25, friends: Mike,Liza 41someFunction('Rick', ['Mike', 'Liza']); 42// name: Rick, age: undefined, friends: Mike,Liza 43someFunction(25, ['Mike', 'Liza']); 44// name: undefined, age: 25, friends: Mike,Liza 45someFunction(['Mike', 'Liza']); 46// name: undefined, age: undefined, friends: Mike,Liza 47 48
A simple method to get type of argument
1get.type('any argument here'); // type of argument 2 3get.type(43); // Number 4get.type(Checker); // Checker
You can check the following types by "is", "as" or javascript, but this looks more readable
1is.arguments(1); // true | false 2as.arguments(1) // 1 | TypeError 3// the same of 4is.arrayObject(1); // true 5 6is.generator(function*() { yield 0 }) // true | false 7as.generator(function*() { yield 0 }) // function*() { yield 0 } | TypeError 8// the same of 9is.iterator(function*() { yield 0 }) // true | false 10as.iterator(function*() { yield 0 }) // function*() { yield 0 } | TypeError 11 12is.NodeJs() // true | false 13as.NodeJs() // process | TypeError 14// the same of 15process !== undefined // true 16 17is.browser() // true 18ss.browser() // navigator | TypeError 19// the same of 20navigator !== undefined // true 21 22is.Chrome() // true 23as.Chrome() // navigator | TypeError 24// the same of 25navigator.userAgent.includes('Chrome') // true 26
1Enum.init('enum object here')
Use increment
1Enum.init({ 2 RED: 0, 3 GREEN: Enum.inc, 4 BLUE: Enum.inc, 5}); 6 7// Enum { 8// '0': 'RED', 9// '1': 'GREEN', 10// '2': 'BLUE', 11// RED: 0, 12// GREEN: 1, 13// BLUE: 2 14// }
Use decrement
1Enum.init({ 2 ROOF: 2, 3 FLOOR: Enum.dec, 4 BASEMENT: Enum.dec, 5}); 6// Enum { 7// '0': 'BASEMENT', 8// '1': 'FLOOR', 9// '2': 'ROOF', 10// ROOF: 2, 11// FLOOR: 1, 12// BASEMENT: 0 13// }
Use both
1Enum.init({ 2 RED: 0, 3 GREEN: Enum.inc, 4 BLUE: Enum.inc, 5 ROOF: 6, 6 FLOOR: Enum.dec, 7 BASEMENT: Enum.dec, 8}); 9// Enum { 10// '0': 'RED', 11// '1': 'GREEN', 12// '2': 'BLUE', 13// '4': 'BASEMENT', 14// '5': 'FLOOR', 15// '6': 'ROOF', 16// RED: 0, 17// GREEN: 1, 18// BLUE: 2, 19// ROOF: 6, 20// FLOOR: 5, 21// BASEMENT: 4 22// }
Use with step
1Enum.init({ 2 [Enum.step]: 10, // ['Enum.step'] the same but with a quotes 3 RED: Enum.inc, 4 GREEN: Enum.inc, 5 BLUE: Enum.inc, 6}); 7 8// Enum { 9// '10': 'RED', 10// '20': 'GREEN', 11// '30': 'BLUE', 12// RED: 10, 13// GREEN: 20, 14// BLUE: 30 15// } 16Enum.init({ 17 [Enum.step]: 10, 18 ROOF: Enum.dec, 19 FLOOR: 30, 20 BASEMENT: Enum.dec, 21}); 22// Enum { 23// '10': 'ROOF', 24// '20': 'BASEMENT', 25// '30': 'FLOOR', 26// ROOF: 10, 27// FLOOR: 30, 28// BASEMENT: 20 29// }
Check the Enum type like this
1as.Enum(enumExample) && as.enum(enumExample);
You can integrate any feature you want.
1import { Checker, BaseInterface, Enum, JSON5 } from 'checker-as-is'; 2import axios from "axios"; 3 4const integrate = { 5 up: async function (url) { 6 const result = await axios.get(url); 7 if(result.status === 200) return 'Ok'; 8 else throw new TypeError('url is down'); 9 } 10}; 11 12const { multi, strict, as, is } = new Checker(integrate); 13const isUrl = as; 14 15async function example(arg, arg2, arg3, 16 type = [as.string(arg), as.number(arg2), as.boolean(arg3)]) { 17 await isUrl.up('https://google.com'); 18 console.log(type); 19}; 20 21await example('text', 2, true) 22// [ 'text', 2, true ] 23 24await isUrl?.up('https://not-google.com'); 25// TypeError: url is down
To change error message you can reload Checker.errorMsg. If you want to disable throwing error please set checker.disabled = true.
1const checker = new Checker(integrate); 2checker.errorMsg = (params)=> `${params[2] || (params[0]?.constructor 3 ? params[0].constructor.name 4 :params[0]) 5 } , really? I'm not sure that is a(an) ${params[1]}`; 6 7const { multi, strict, as, is } = checker; 8checker.disabled = true; 9// TypeError: Number, really? I'm not sure that is a(an) string
No dependencies except of json5 in vendor folder, pure javascript code. No selfish syntax, if you know javascript you can write code without any challenges.
No vulnerabilities found.
No security vulnerabilities found.