@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, all=false)
will return jet type name of variable any
- Arguments
- any: any variable
- all: boolean (return all types?)
- Return
- all=false: top type of variable
- all=true: array with all types of variable
- Example
- jet.type([]) === "Array";
- jet.type(NaN, true) === ["NaN", "Number"];
jet.define(name, constructor, options={})
Defining custom types for detecting, creating and copying
Same jet methods and plugins 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)
- plugins: object (functions that will be appended to constructor and prototype)
- extend: _boolean (false=turn off extension of constructor and prototype)
- extendConstructor: _boolean (false=turn off extension of constructor)
- extendPrototype: _boolean (false=turn off extension of prototype)
- Return
- Example
- jet.type.define("Arr", Array, { create:x=>new Array(x), copy:x=>Array.from(x) } );
- jet.type.define("Ele", Element, { plugins:{ find:query=>document.querySelector(query) } });
jet.isMapable(any)
Return true on any type of variable that has mapable=true on its type definition
- Arguments
- 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
- Return
- true when variable is runnable
- Example
- jet.isRunnable([]) === false
- jet.isRunnable({}) === false;
- jet.isRunnable(()=>{}) === true;
jet.copy(any)
Will create copy of instance
- Arguments
- Return
- new instance or the old if there isn't defined copy function
- Example
- jet.copy({a:1}) == Object.assign({}, {a:1});
- jet.copy(["foo", "bar"]) == Array.from(["foo", "bar"]);
Constructor/Prototype methods
These methods acumulate main funcstionality.
After 'jet.define(name, consturctor)' is called, those methods are attached to 4 different endpoints.
- Global dynamic: jet.method(name, ...args)
- Global static: _jet.method.name(...args)
- Constructor: constructor.jet.method(...args)
- Prototype: instance.jet.method(...args)
jet.is(name, any, inclusive=false)
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
- inclusive: boolean
- Return
- inclusive=true: true when the type is included in result of jet.type all=true
- Example
- jet.is.Array([]) === true;
- jet.is.Object([]) === false;
- jet.is.Array([], true) === true;
- jet.is.RegExp(RegExp()) === true;
jet.isFull(any)
Catching empty mapable objects and NaN
- Arguments
- 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
- 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";
jet.dig(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.dig({foo:["bar"]}, "foo.0") == "bar";
- jet.dig({foo:["bar"]}, ["foo", 1], "foo") == "foo";
jet.put(any, path, val, force=true)
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)
- Return
- Example
- jet.put({}, "foo.0", "bar", true) == {foo:["bar"]};
jet.forEach(any, fce, deep, path) / .map(any, fce, deep, path)
Map any mapable object by default: Object, Array, Set, Map, Pool
- Arguments
- any: any mapable variable
- fce: function(val, path+key, parent, parentpath) (handler)
- deep: boolean or function (true=recursive maping; function=custom recursive maping)
- deep: boolean (recursive maping)
- 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"};
jet.run(any, ...args)
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