@randajan/jet-core
v4.1.0
Published
Ecosystem of types and related usefull tools.
Downloads
39
Readme
@randajan/jet-core
Goal is to create ecosystem for javascript types with possibility to define custom types. This package provide easy and deep objects map and comparing, generic creating, filtering by types, content & other stuff
Install
npm install @randajan/jet-core
or
yarn add @randajan/jet-core
Main methods
These methods, are exclusive for default jet export
jet(any, strict=true)
will return jet type name of variable any
- Arguments
- any: any variable
- strict: boolean (true = return closest type)
- Return
- all=false: top type of variable
- all=true: array with all types of variable
- Example
- jet.type([]) === "Array";
- jet(new (class {})(), true) === undefined;
- jet(new (class {})(), false) === "Object";
jet.define(name, constructor, options={})
Defining custom types for detecting, creating and copying Same jet methods will be attached to jet.name, to constructor.jet.() and to prototype.jet.*()
- Arguments
- name: string (name of the type)
- constructor: class
- options: object
- 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)
- extend: _boolean (false=turn off extension of constructor and prototype)
- extendConstructor: _boolean or object (false=turn off extension of constructor)
- extendPrototype: _boolean or object (false=turn off extension of prototype)
- Return
- constructor
- Example
- jet.type.define("Array", Array, { create:x=>new Array(x), copy:x=>Array.from(x) } );
- jet.type.define("Element", Element, { extendConstructor:{ find:query=>document.querySelector(query) } });
jet.isMapable(any, strict=true)
Return true on any type of variable that has mapable=true on its type definition
- Arguments
- any: any variable
- strict: boolean (true = is closest type mapable)
- Return
- true when variable is mapable
- Example
- jet.isMapable([]) === true
- jet.isMapable({}) === true;
- jet.isMapable("foo") === false;
jet.isRunnable(any)
Return true if any typeof === function
- Arguments
- any: any variable
- Return
- true when variable is runnable
- Example
- jet.isRunnable([]) === false
- jet.isRunnable({}) === false;
- jet.isRunnable(()=>{}) === true;
Constructor/Prototype methods
These methods acumulate main funcstionality. After 'jet.define(name, consturctor)' is called, those methods are attached to 3 different endpoints.
- Global dynamic: jet.method(name, ...args)
- Global static: _jet.method.name(...args)
- Constructor: constructor.jet.method(...args)
jet.is(name, any, strict=true)
Check the passed type with result. Endpoint 'jet.is(name, ...a)' also work like typeof and instanceof
- Arguments
- name: string (name of the type)
- any: any variable
- strict: boolean (true = is instanceof)
- Example
- jet.is.Array([]) === true;
- jet.is.Object([]) === false;
- jet.is.Object([], true) === true;
- jet.is.RegExp(RegExp()) === true;
jet.isFull(any)
Catching empty mapable objects and NaN
- Arguments
- any: any variable
- Return
- true when variable is full
- Example
- jet.isFull([]) === false;
- jet.isFull({foo:bar}) === true;
jet.create(name, ...args)
Will create instance requested constructor (use without "new")
- Arguments
- name: string (name of the type)
- ...args: will be passed to the creating function
- Return
- new instance
- Example
- jet.create.Array("foo", "bar") == ["foo", "bar"];
- jet.create.Object() == {};
jet.rnd(name, ...args)
Will create instance with random value
- Arguments
- name: string (name of the type)
- ...args: will be passed to the defined rnd method
- Return
- new instance with random value
jet.full(...any) / .only(name, ...any) / .tap(name, ...any) / .pull(name, ...any)
Used for selecting, filtering, creating or copying variables
- Arguments
- name: string (name of the type)
- ...any: any variables (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.only.Sring(1, "foo", [], ["bar"], {foo:"bar"}) == "foo";
- jet.tap.Array(1, "foo", [], ["bar"], {foo:"bar"}) == [];
- jet.full.Array(1, "foo", [], ["bar"], {foo:"bar"}) == ["bar"];
- jet.only.RegExp(1, "foo", [], ["bar"], {foo:"bar"}) == null;
- jet.tap.RegExp(1, "foo", [], ["bar"], {foo:"bar"}) == RegExp();
- jet.pull.Object(1, "foo", [], ["bar"], {foo:"bar"}) == {foo:"bar"}
jet.vals(any) / .keys(any) / .pairs(any) / .get(any, key) / .set(any, key, val) / .rem(any, key)
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.vals({foo:"bar"}) === ["bar"];
- jet.keys({foo:"bar"}) === ["foo"];
- jet.entries({foo:"bar"}) === [["foo"], ["bar"]];
- jet.get({foo:"bar"}, "foo") === "bar";
Extra methods
jet.uid(length, pattern)
- Arguments
- length: number (desired length)
- pattern: string (list of characters)
- Return
- string (pseudo random)
- Example
- jet.uid(8, "a") === "aaaaaaaa"
jet.copy(any, deep=false, copyUnmapable=false)
Will create copy of instance
- Arguments
- any: any variable
- deep: boolean (deep copy of mapable objects)
- copyUnmapable: boolean (copy even unmapable values in the deep nested structure)
- Return
- new instance or the old if there wasn't defined copy function
- Example
- x = [{a:Symbol("test")}]; y = jet.copy(x); console.log(x===y, x[0]===y[0], x[0].a===y[0].a) // false true true
- x = [{a:Symbol("test")}]; y = jet.copy(x, true); console.log(x===y, x[0]===y[0], x[0].a===y[0].a) // false false true
- x = [{a:Symbol("test")}]; y = jet.copy(x, true, true); console.log(x===y, x[0]===y[0], x[0].a===y[0].a) // false false false
jet.deflate(any, includeMapable=false)
Will deflate nested structure to the flat object where indexes are whole original dot-separated paths
- Arguments
- any: any variable
- includeMapable: boolean
- Return
- includeMapable=false: object (alle values exclude mapable values in original structure)
- includeMapable=true: object (all values include mapable values in original structure)
- Example
- jet.deflate({a:{b:["c"]}}) === {"a.b.0": "c"}
- jet.deflate({a:{b:["c"]}}, true) === {"a.b.0": "c"}
- jet.deflate({a:["c"]}, true) === { "a":["c"], "a.0":"c", "":{"a":["c"]} }
jet.compare(anyA, anyB, diffList=false)
Will compare two variables include deep nested objects
- Arguments
- anyA: any variable
- anyB: any variable
- diffList: boolean
- Return
- diffList=false: boolean (false if the content is same)
- diffList=true: boolean (list of differencies)
- Example
- jet.compare({a:{b:["c"]}}, {a:{b:["c"]}}) === true
- jet.compare({a:{b:["c"]}}, {a:{b:["d"]}}) === false
- jet.compare({a:{b:["c"]}}, {a:{b:["d"]}}, true) === ["a.b.0"];
jet.melt(any, comma)
Will join any values even values of deep nested structure
- Arguments
- any: any variable
- comma: string ()
- Return
- string (comma separated values)
- Example
- jet.melt({a:["b", "c"], d:"e"}, ", ") === "b, c, e"
jet.dig(any, path, reductor)
Exploring nested structure by path
- Arguments
- any: any mapable variable
- path: string or Array (even nested Array)
- reductor: function(next, parent, dir+key, dir, key, isEnd)
- Return
- result of first iteration of reductor
jet.digIn(any, path, val, force=true, reductor)
Return value from deep nested object
- Arguments
- any: any mapable variable
- path: string or Array (even nested Array)
- val: any
- force: boolean (create path if not exist)
- reductor: function(next, parent, dir+key, dir, key, isEnd)
- Return
- any
- Example
- jet.digIn({}, "foo.0", "bar", true) == {foo:["bar"]};
jet.digOut(any, path, def)
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.digOut({foo:["bar"]}, "foo.0") == "bar";
- jet.digOut({foo:["bar"]}, ["foo", 1], "foo") == "foo";
jet.run(any, ...args)
Will run every function that will discover
- Arguments
- any: any (function || array/object with functions
- ...args: arguments will be passed to every call
- Return
- any=function: result of function
- any=array/object: array of all results
- Example
- jet.run(_=>console.log("foo")) console: "foo"
jet.forEach(any, fce, deep, dir) / .map(any, fce, deep, dir)
Map any mapable object by default: Object, Array, Set, Map, Pool
- Arguments
- any: any mapable variable
- fce: function(val, dir+key, dir, key) (handler)
- deep: boolean or function (true=recursive maping; function=custom recursive maping)
- dir: string (base dir of structure)
- Return
- forEach: flat array with result from handler function
- map: copy of structure with result from handler function
- Example
- jet.forEach({foo:"bar"}, =>) == ["bar"];
- jet.map({foo:"bar"}, =>) == {foo:"bar"};
License
MIT © randajan