@randajan/jet-core
Advanced tools
Comparing version 2.2.0 to 3.0.0
@@ -24,3 +24,3 @@ var __defProp = Object.defineProperty; | ||
var byName = {}; | ||
var byConstructor = /* @__PURE__ */ new Map(); | ||
var byPrototype = /* @__PURE__ */ new Map(); | ||
var constructorByName = {}; | ||
@@ -35,38 +35,49 @@ var defaultThrow = (msg, name) => `jet${name ? ` type '${name}'` : ""} ${msg}`; | ||
var getDefByName = (name) => byName[name]; | ||
var getDefByConst = (constructor) => { | ||
const list = byConstructor.get(constructor); | ||
var getDefByProto = (prototype) => { | ||
const list = byPrototype.get(prototype); | ||
return list ? list[0] : void 0; | ||
}; | ||
var getByInst = (any, all = false, withDefinition = false) => { | ||
const r = all ? [] : void 0; | ||
if (any == null) { | ||
return r; | ||
var getByInst = (any2, def, withDef = true) => { | ||
if (!def.is || def.is(any2)) { | ||
return withDef ? def : def.name; | ||
} | ||
const list = byConstructor.get(any.constructor); | ||
}; | ||
var findByProto = (any2, proto, withDef = true) => { | ||
const list = byPrototype.get(proto); | ||
if (!list) { | ||
return r; | ||
return; | ||
} | ||
if (list.length === 1) { | ||
return getByInst(any2, list[0], withDef); | ||
} | ||
for (const def of list) { | ||
if (def.is && !def.is(any)) { | ||
continue; | ||
} | ||
const e = withDefinition ? def : def.name; | ||
const r = getByInst(any2, def, withDef); | ||
if (r) { | ||
r.push(e); | ||
} else { | ||
return e; | ||
return r; | ||
} | ||
} | ||
}; | ||
var findByInst = (any2, strict, withDef = true) => { | ||
if (any2 == null) { | ||
return; | ||
} | ||
if (strict) { | ||
return findByProto(any2, any2.__proto__, withDef); | ||
} | ||
let r, p = any2; | ||
do { | ||
r = findByProto(any2, p = p.__proto__, withDef); | ||
} while (p && r === void 0); | ||
return r; | ||
}; | ||
var getDefByInst = (any, all = false) => getByInst(any, all, true); | ||
var getNameByInst = (any, all = false) => getByInst(any, all, false); | ||
var getDefByInst = (any2, strict = true) => findByInst(any2, strict, true); | ||
var getNameByInst = (any2, strict = true) => findByInst(any2, strict, false); | ||
var register = (def) => { | ||
byName[def.name] = def; | ||
Object.defineProperty(constructorByName, def.name, { enumerable: true, value: def.constructor }); | ||
const list = byConstructor.get(def.constructor); | ||
const list = byPrototype.get(def.prototype); | ||
if (list) { | ||
list.unshift(def); | ||
} else { | ||
byConstructor.set(def.constructor, [def]); | ||
byPrototype.set(def.prototype, [def]); | ||
} | ||
@@ -80,8 +91,8 @@ }; | ||
var magic = ["only", "full", "tap", "pull", "is", "to", "copy", "rnd"]; | ||
var isInstance = (any) => { | ||
const t = typeof any; | ||
return any != null && (t === "function" || t === "object"); | ||
var isInstance = (any2) => { | ||
const t = typeof any2; | ||
return any2 != null && (t === "function" || t === "object"); | ||
}; | ||
var isInstanceOf = (constructor, any) => any instanceof constructor; | ||
var is = (name, any, inclusive = false) => { | ||
var isInstanceOf = (constructor, any2) => any2 instanceof constructor; | ||
var is = (name, any2, strict = true) => { | ||
if (!name) { | ||
@@ -92,21 +103,27 @@ return false; | ||
if (def) { | ||
if (any == null || any.constructor !== def.constructor) { | ||
if (any2 == null) { | ||
return false; | ||
} | ||
return inclusive || name === getNameByInst(any); | ||
if (strict && any2.__proto__ !== def.prototype) { | ||
return false; | ||
} | ||
if (!strict && !(any2 instanceof def.constructor)) { | ||
return false; | ||
} | ||
return !def.is || def.is(any2); | ||
} | ||
const nt = typeof name; | ||
if (nt === "string") { | ||
return typeof any === name; | ||
return typeof any2 === name; | ||
} | ||
if (any == null || nt !== "function" && nt !== "object") { | ||
if (any2 == null || nt !== "function" && nt !== "object") { | ||
return false; | ||
} | ||
return inclusive ? any instanceof name : any.constructor === name; | ||
return strict ? any2.constructor === name : any2 instanceof name; | ||
}; | ||
var isFull = (any, vals) => { | ||
var isFull = (any2, vals) => { | ||
if (!vals) { | ||
return any === false || any === 0 || !!any; | ||
return any2 === false || any2 === 0 || !!any2; | ||
} | ||
for (let v of vals(any)) { | ||
for (let v of vals(any2)) { | ||
if (v != null) { | ||
@@ -118,27 +135,27 @@ return true; | ||
}; | ||
var _touch = (def, op, err, ...args) => { | ||
if (def[op]) { | ||
return def[op](...args); | ||
} | ||
if (err) { | ||
throwError(`undefined operation '${op}' - unavailable for this type`, def.name); | ||
} | ||
}; | ||
var touch = (name, op, err, ...args) => { | ||
const def = getDefByName(name); | ||
if (!def) { | ||
if (err) { | ||
throwError(`unable execute '${op}' - type unknown`, name); | ||
} | ||
return; | ||
if (def) { | ||
return _touch(def, op, err, any, ...args); | ||
} | ||
if (!def[op]) { | ||
if (err) { | ||
throwError(`undefined operation '${op}' - unavailable for this type`, name); | ||
} | ||
return; | ||
if (err) { | ||
throwError(`unable execute '${op}' - type unknown`, name); | ||
} | ||
return def[op](...args); | ||
}; | ||
var touchBy = (any, op, err, ...args) => { | ||
const name = getNameByInst(any); | ||
if (!name) { | ||
if (err) { | ||
throwError(`unable execute '${op}' - missing type of '${any}'`); | ||
} | ||
return; | ||
var touchBy = (any2, op, err, ...args) => { | ||
const def = getDefByInst(any2, false); | ||
if (def) { | ||
return _touch(def, op, err, any2, ...args); | ||
} | ||
return touch(name, op, err, any, ...args); | ||
if (err) { | ||
throwError(`unable execute '${op}' - missing type of '${any2}'`); | ||
} | ||
}; | ||
@@ -171,3 +188,3 @@ var factory = (name, mm, ...args) => { | ||
}; | ||
var to = (name, any, ...args) => { | ||
var to = (name, any2, ...args) => { | ||
const def = getDefByName(name); | ||
@@ -177,3 +194,3 @@ if (!def) { | ||
} | ||
const at = getDefByInst(any); | ||
const at = getDefByInst(any2, false); | ||
if (!at) { | ||
@@ -183,6 +200,6 @@ return def.create(); | ||
if (def.name === at.name) { | ||
return any; | ||
return any2; | ||
} | ||
const exe = at.to[name] || at.to["*"]; | ||
return exe ? to(name, exe(any, ...args), ...args) : def.create(any); | ||
return exe ? to(name, exe(any2, ...args), ...args) : def.create(any2); | ||
}; | ||
@@ -234,3 +251,3 @@ var toDefine = (from, to3, exe) => { | ||
create = create || ((...a) => new constructor(...a)); | ||
copy = copy || ((any) => any); | ||
copy = copy || ((any2) => any2); | ||
const ec = extendConstructor === false ? null : typeof extendConstructor === "object" ? extendConstructor : {}; | ||
@@ -240,3 +257,3 @@ const ep = extendPrototype === false ? null : typeof extendPrototype === "object" ? extendPrototype : {}; | ||
if (extend !== false) { | ||
const ancestor = getDefByConst(constructor); | ||
const ancestor = getDefByProto(prototype); | ||
if (ancestor) { | ||
@@ -270,4 +287,4 @@ throwWarn(`constructor allready extended as '${ancestor}'. Use option 'extend=false'`, name); | ||
ex(0, "create", create); | ||
ex(0, "is", (any, inclusive = false) => is(name, any, inclusive)); | ||
ex(0, "to", (any, ...a) => to(name, any, ...a)); | ||
ex(0, "is", (any2, strict = true) => is(name, any2, strict)); | ||
ex(0, "to", (any2, ...a) => to(name, any2, ...a)); | ||
ex(0, "only", (...a) => factory(name, 0, ...a)); | ||
@@ -278,3 +295,3 @@ ex(0, "full", (...a) => factory(name, 1, ...a)); | ||
ex(0, "rnd", rnd = rnd || create); | ||
ex(2, "isFull", full = full || ((any) => isFull(any, vals))); | ||
ex(2, "isFull", full = full || ((any2) => isFull(any2, vals))); | ||
if (entries) { | ||
@@ -287,3 +304,3 @@ ex(2, "keys", keys); | ||
ex(2, "rem", rem = rem || ((x, k) => delete x[k])); | ||
ex(2, "getRND", (any, min, max, sqr) => getRND(vals(any), min, max, sqr)); | ||
ex(2, "getRND", (any2, min, max, sqr) => getRND(vals(any2), min, max, sqr)); | ||
} | ||
@@ -323,5 +340,5 @@ register({ name, constructor, prototype, is: is2, create, full, copy, rnd, keys, vals, entries, get, set, rem, to: {} }); | ||
}); | ||
var _each = ({ create, entries, set }, any, fce, deep, dprun, dir, flat) => { | ||
var _each = ({ create, entries, set }, any2, fce, deep, dprun, dir, flat) => { | ||
const res = flat || create(); | ||
for (let [key, val] of entries(any)) { | ||
for (let [key, val] of entries(any2)) { | ||
const path = (dir ? dir + "." : "") + key; | ||
@@ -349,10 +366,10 @@ const def = deep || !fce ? getDefByInst(val) : null; | ||
}; | ||
var _eachInit = (any, fce, deep, dir, flat) => { | ||
const df = getDefByInst(any); | ||
var _eachInit = (any2, fce, deep, dir, flat) => { | ||
const df = getDefByInst(any2); | ||
dir = String.jet.to(dir, "."); | ||
if (df && df.entries) { | ||
return _each(df, any, fce, deep, defs_default.isRunnable(deep), dir, flat); | ||
return _each(df, any2, fce, deep, defs_default.isRunnable(deep), dir, flat); | ||
} | ||
; | ||
const val = fce ? fce(any, dir, "", dir) : df.copy ? df.copy(any) : any; | ||
const val = fce ? fce(any2, dir, "", dir) : df.copy ? df.copy(any2) : any2; | ||
if (flat && val !== void 0) { | ||
@@ -363,4 +380,4 @@ flat.push(val); | ||
}; | ||
var forEach = (any, fce, deep = false, dir = "") => _eachInit(any, fce, deep, dir, []); | ||
var map = (any, fce, deep = false, dir = "") => _eachInit(any, fce, deep, dir); | ||
var forEach = (any2, fce, deep = false, dir = "") => _eachInit(any2, fce, deep, dir, []); | ||
var map = (any2, fce, deep = false, dir = "") => _eachInit(any2, fce, deep, dir); | ||
var reducer = (reductor) => { | ||
@@ -370,3 +387,3 @@ let i = 0, next; | ||
}; | ||
var dig = (any, path, reductor) => { | ||
var dig = (any2, path, reductor) => { | ||
const pa = String.jet.to(path, ".").split("."); | ||
@@ -377,17 +394,17 @@ const end = pa.length - 1; | ||
return reductor(next, parent, (dir ? dir + "." : "") + pa[index], dir, pa[index], index === end); | ||
})(any); | ||
})(any2); | ||
}; | ||
var digOut = (any, path, def) => { | ||
var digOut = (any2, path, def) => { | ||
path = String.jet.to(path, "."); | ||
if (!path) { | ||
return any; | ||
return any2; | ||
} | ||
for (let p of path.split(".")) { | ||
if ((any = defs_default.get(any, p, false)) == null) { | ||
if ((any2 = defs_default.get(any2, p, false)) == null) { | ||
return def; | ||
} | ||
} | ||
return any; | ||
return any2; | ||
}; | ||
var digIn = (any, path, val, force = true, reductor = void 0) => { | ||
var digIn = (any2, path, val, force = true, reductor = void 0) => { | ||
const step = (next, parent, path2, dir, key, isEnd) => { | ||
@@ -412,5 +429,5 @@ let df = getDefByInst(parent); | ||
}; | ||
return dig(any, path, !defs_default.isRunnable(reductor) ? step : (next, parent, path2, dir, key, isEnd) => reductor((parent2) => step(next, parent2, path2, dir, key, isEnd), parent, path2, dir, key, isEnd)); | ||
return dig(any2, path, !defs_default.isRunnable(reductor) ? step : (next, parent, path2, dir, key, isEnd) => reductor((parent2) => step(next, parent2, path2, dir, key, isEnd), parent, path2, dir, key, isEnd)); | ||
}; | ||
var deflate = (any, includeMapable = false) => { | ||
var deflate = (any2, includeMapable = false) => { | ||
const flat = {}; | ||
@@ -424,3 +441,3 @@ const add = (v, p) => { | ||
}; | ||
forEach(any, add, includeMapable ? deep : true); | ||
forEach(any2, add, includeMapable ? deep : true); | ||
return flat; | ||
@@ -438,3 +455,3 @@ }; | ||
}; | ||
var _assign = (overwriteArray, to3, ...any) => { | ||
var _assign = (overwriteArray, to3, ...any2) => { | ||
const r = { to: to3 }; | ||
@@ -455,3 +472,3 @@ const flat = deflate(r.to, true); | ||
}; | ||
for (const a of any) { | ||
for (const a of any2) { | ||
forEach(a, add, !!overwriteArray || acumulate); | ||
@@ -462,9 +479,9 @@ } | ||
var assign = (to3, from, overwriteArray = true) => _assign(overwriteArray, to3, from); | ||
var merge = (...any) => _assign(false, {}, ...any); | ||
var melt = (any, comma) => { | ||
var merge = (...any2) => _assign(false, {}, ...any2); | ||
var melt = (any2, comma) => { | ||
let j = "", c = String.jet.to(comma); | ||
if (!defs_default.isMapable(any)) { | ||
return String.jet.to(any, c); | ||
if (!defs_default.isMapable(any2)) { | ||
return String.jet.to(any2, c); | ||
} | ||
forEach(any, (v) => { | ||
forEach(any2, (v) => { | ||
v = melt(v, c); | ||
@@ -522,11 +539,11 @@ j += v ? (j ? c : "") + v : ""; | ||
to, | ||
isFull: (any) => { | ||
const def = getDefByInst(any); | ||
return def ? def.full(any) : isFull(any); | ||
isFull: (any2) => { | ||
const def = getDefByInst(any2, false); | ||
return def ? def.full(any2) : isFull(any2); | ||
}, | ||
isMapable: (any) => { | ||
const def = getDefByInst(any); | ||
isMapable: (any2, strict = true) => { | ||
const def = getDefByInst(any2, strict); | ||
return def ? !!def.entries : false; | ||
}, | ||
isRunnable: (any) => typeof any === "function", | ||
isRunnable: (any2) => typeof any2 === "function", | ||
full: (...a) => factory(null, 1, ...a), | ||
@@ -538,23 +555,22 @@ only: (name, ...a) => factory(name, 0, ...a), | ||
rnd: (name, ...a) => touch(name, "rnd", true, ...a), | ||
copy: (any, deep = false, copyUnmapable = false) => deep ? defs_default.map(any, copyUnmapable ? void 0 : (_) => _, true) : touchBy(any, "copy"), | ||
keys: (any, throwError2 = false) => touchBy(any, "keys", throwError2) || [], | ||
vals: (any, throwError2 = false) => touchBy(any, "vals", throwError2) || [], | ||
entries: (any, throwError2 = false) => touchBy(any, "entries", throwError2) || [], | ||
get: (any, key, throwError2 = false) => touchBy(any, "get", throwError2, key), | ||
set: (any, key, val, throwError2 = false) => touchBy(any, "set", throwError2, key, val), | ||
rem: (any, key, throwError2 = true) => touchBy(any, "rem", throwError2, key), | ||
getRND: (any, min, max, sqr) => { | ||
const def = getDefByInst(any); | ||
if (def.vals) { | ||
any = def.vals(any); | ||
} else if (typeof any !== "string") { | ||
return; | ||
copy: (any2, deep = false, copyUnmapable = false) => deep ? defs_default.map(any2, copyUnmapable ? void 0 : (_) => _, true) : touchBy(any2, "copy"), | ||
keys: (any2, throwError2 = false) => touchBy(any2, "keys", throwError2) || [], | ||
vals: (any2, throwError2 = false) => touchBy(any2, "vals", throwError2) || [], | ||
entries: (any2, throwError2 = false) => touchBy(any2, "entries", throwError2) || [], | ||
get: (any2, key, throwError2 = false) => touchBy(any2, "get", throwError2, key), | ||
set: (any2, key, val, throwError2 = false) => touchBy(any2, "set", throwError2, key, val), | ||
rem: (any2, key, throwError2 = true) => touchBy(any2, "rem", throwError2, key), | ||
getRND: (any2, min, max, sqr) => { | ||
const def = getDefByInst(any2); | ||
if (def && def.vals) { | ||
any2 = def.vals(any2); | ||
} else if (typeof any2 === "string") { | ||
return getRND(any2, min, max, sqr); | ||
} | ||
return getRND(any, min, max, sqr); | ||
}, | ||
run: (any, ...args) => { | ||
if (defs_default.isRunnable(any)) { | ||
return [any(...args)]; | ||
run: (any2, ...args) => { | ||
if (defs_default.isRunnable(any2)) { | ||
return [any2(...args)]; | ||
} | ||
return defs_default.map(any, (f) => defs_default.run(f, ...args)); | ||
return defs_default.map(any2, (f) => defs_default.run(f, ...args)); | ||
}, | ||
@@ -707,3 +723,3 @@ ...pile_exports, | ||
var String_default = jet_default.define("String", String, { | ||
create: (any) => any == null ? "" : String(any), | ||
create: (any2) => any2 == null ? "" : String(any2), | ||
rnd: (min, max, sqr) => { | ||
@@ -710,0 +726,0 @@ const c = ["bcdfghjklmnpqrstvwxz", "aeiouy"], p = c[0].length / (c[0].length + c[1].length); |
{ | ||
"name": "@randajan/jet-core", | ||
"version": "2.2.0", | ||
"version": "3.0.0", | ||
"description": "Ecosystem of types and related usefull tools.", | ||
@@ -5,0 +5,0 @@ "repository": "randajan/jet-core", |
@@ -23,3 +23,3 @@ # @randajan/jet-core | ||
### __jet(any, all=false)__ | ||
### __jet(any, strict=true)__ | ||
_will return jet type name of variable any_ | ||
@@ -29,3 +29,3 @@ | ||
* any: _any variable_ | ||
* all: _boolean (return all types?)_ | ||
* strict: _boolean (true = return closest type)_ | ||
* Return | ||
@@ -36,3 +36,4 @@ * all=false: _top type of variable_ | ||
* jet.type([]) === "Array"; | ||
* jet.type(NaN, true) === ["NaN", "Number"]; | ||
* jet(new (class {})(), true) === undefined; | ||
* jet(new (class {})(), false) === "Object"; | ||
@@ -67,3 +68,3 @@ ### __jet.define(name, constructor, options={})__ | ||
### __jet.isMapable(any)__ | ||
### __jet.isMapable(any, strict=true)__ | ||
_Return true on any type of variable that has mapable=true on its type definition_ | ||
@@ -73,2 +74,3 @@ | ||
* any: _any variable_ | ||
* strict: _boolean (true = is closest type mapable)_ | ||
* Return | ||
@@ -116,3 +118,3 @@ * _true when variable is mapable_ | ||
### __jet.is(name, any, inclusive=false)__ | ||
### __jet.is(name, any, strict=true)__ | ||
_Check the passed type with result. Endpoint 'jet.is(name, ...a)' also work like typeof and instanceof_ | ||
@@ -123,9 +125,7 @@ | ||
* any: _any variable_ | ||
* inclusive: _boolean_ | ||
* Return | ||
* inclusive=true: _true when the type is included in result of jet.type all=true_ | ||
* strict: _boolean (true = is instanceof)_ | ||
* Example | ||
* jet.is.Array([]) === true; | ||
* jet.is.Object([]) === false; | ||
* jet.is.Array([], true) === true; | ||
* jet.is.Object([], true) === true; | ||
* jet.is.RegExp(RegExp()) === true; | ||
@@ -132,0 +132,0 @@ |
Sorry, the diff of this file is not supported yet
117779
1111