@randajan/jetpack
Collection of devtools
Install
npm install --save @randajan/jetpack
About
Custom types, maping objects, generic creating, filtering by types & other stuff
Main content
jet.type
Superstructure of native typeof and instanceof methods
- Arguments
- any: any variable
- all: boolean (return all types?)
- Return
- all=false || undefined: top type of variable
- all=true: array with all types of variable sorted by its priority
- Example
- jet.type([], true) === ["arr", "object"];
- jet.type(RegExp()) === "regex";
jet.type.define
Defining custom types for detecting, creating and copying
- Arguments
- name: string (name of the type)
- constructor: class
- opt: object
- rank: number (>= 0)
- create: function (creating new instance)
- is: function (verify type of variable)
- full: function (check if variable is full)
- copy: function (perform copy)
- rnd: function (create with random content)
- keys: function (array of keys for mapable types)
- vals: function (array of values for mapable types)
- pairs: function (array of entries for mapable types)
- get: function (get key for mapable types)
- set: function (set key for mapable types)
- rem: function (rem key for mapable types)
- custom: object (functions that should be appended to jet.*)
- Return
- object similiar to jet.* when successfully defined
- Example
- jet.type.define("arr", Array, { rank:-1, create:x=>new Array(x), copy:x=>Array.from(x) } );
- jet.type.define("ele", Element, { rank:-1 }, { find:query=>document.querySelector(query) });
jet.type.is
Check the passed type with result from instanceof compare || jet.type
- Arguments
- type: string (type name) || class (for compare instanceof)
- any: any variable
- inclusive: boolean
- Return
- type=class: any instanceof type
- inclusive=true: true when the type is included in result of jet.type all=true
- Example
- jet.is(Array, []) === true;
- jet.is("arr", []) === true;
- jet.is("obj", []) === false;
- jet.is("obj", [], true) === true;
- jet.is("regex", RegExp()) === true;
jet.type.is.map
Return true on any type of variable that has mapable=true on its type definition
- Arguments
- any: any variable
- Return
- true when variable is mapable
- Example
- jet.is.map([]) === true
- jet.is.map({}) === true;
- jet.is.map("foo") === false;
jet.type.is.full
Catching empty mapable objects and NaN
- Arguments
- any: any variable
- Return
- true when variable is full
- Example
- jet.is.full([]) === false;
- jet.is.full({foo:bar}) === true;
jet.*
Will create instance requested * type (use without "new")
- Arguments
- ...args: will be passed to the creating function
- Return
- new instance
- Example
- jet.arr("foo", "bar") == ["foo", "bar"];
- jet.obj() == {};
- jet.ele() == ;
jet.*.is / .is.full
Same as jet.type.is && jet.type.is.full but without first argument
jet.*.copy
Will create copy of instance
- Arguments
- any: any variable
- Return
- new instance or the old if there isn't defined copy function
- Example
- jet.obj.copy({a:1}) == Object.assign({}, {a:1});
- jet.arr.copy(["foo", "bar"]) == Array.from(["foo", "bar"]);
jet.*.only / .tap / .full / .pull
Used for selecting, filtering, creating or copying variables
- Arguments
- ...args: any variable (will be tested in order until the type will match)
- Return
- only: undefined when there is no match
- full: same as only but variable must be full
- tap: same as only but try to create the type when there is no match
- pull: same as tap but try to copy variable if there is match
- Example
- jet.str.only(1, "foo", [], ["bar"], {foo:"bar"}) == "foo";
- jet.arr.tap(1, "foo", [], ["bar"], {foo:"bar"}) == [];
- jet.arr.full(1, "foo", [], ["bar"], {foo:"bar"}) == ["bar"];
- jet.regex.only(1, "foo", [], ["bar"], {foo:"bar"}) == null;
- jet.regex.tap(1, "foo", [], ["bar"], {foo:"bar"}) == RegExp();
- jet.obj.pull(1, "foo", [], ["bar"], {foo:"bar"}) == {foo:"bar"}
jet.map.vals / .keys / .pairs / .get / .set / .rem
Handle mapable objects (it requires defined type)
- Arguments
- any: any variable
- key: any variable (usually string or number)
- val: any variable (used just for for set function)
- Return
- result from perform operation against the defined type_
- Example
- jet.map.get({foo:"bar"}, "bar") === "bar";
jet.map.dig
Return value from deep nested object
- Arguments
- any: any mapable variable
- path: string or Array (even nested Array)
- def: any
- Return
- find value or def when no value was found
- Example
- jet.map.dig({foo:["bar"]}, "foo.0") == "bar";
- jet.map.dig({foo:["bar"]}, ["foo", 1], "foo") == "foo";
jet.map.it / .of
Map any mapable object by default: Object, Array, Set, Map, Pool
- Arguments
- any: any mapable variable
- fce: function(val, key, path) (handler)
- deep: boolean (recursive maping)
- Return
- it: flat array containing all items
- of: copy of structure with result from handler function
- Example
- jet.map.it({foo:"bar"}, =>) == 1;
- jet.map.of({foo:"bar"}, =>"foo"+) == {foo:"foobar"};
jet.fce.run
Will run every function that will discover without collecting results
- Arguments
- any: any (function || array/object with functions
- ...args: arguments will be passed to every call
- Return
- any=function: true when it was run successfully
- any=array/object: count of succesfully runned functions
- Example
- jet.fce.run(=>console.log()) === true console: "foo"
License
MIT © randajan