datx-utils
Advanced tools
Comparing version 1.0.0 to 2.0.0-beta.0
export declare const DEFAULT_TYPE = "__DEFAULT_TYPE__"; | ||
export declare const META_FIELD = "__META__"; | ||
export declare const DATX_META: unique symbol; |
@@ -0,1 +1,2 @@ | ||
export declare function reducePrototypeChain<T, U>(obj: U, reduceFn: (state: T, item: U) => T, initialValue: T): T; | ||
/** | ||
@@ -30,2 +31,9 @@ * Map a single item or an array of items | ||
export declare function isFalsyArray(value: any): boolean; | ||
export declare function getMetaObj(obj: Record<string, any>): Record<string, any>; | ||
export declare function setMeta<T = any>(obj: Record<string, any>, key: string, value: T): void; | ||
export declare function getMeta<T = any>(obj: Record<string, any>, key: string, defaultValue: T, includeChain?: boolean, mergeChain?: boolean): T; | ||
export declare function getMeta<T = any>(obj: Record<string, any>, key: string, defaultValue?: T, includeChain?: boolean, mergeChain?: boolean): T | undefined; | ||
export declare function mergeMeta(obj: Record<string, any>, newMeta: Record<string, any>): Record<string, any>; | ||
declare type Getter<T> = () => T; | ||
declare type Setter<T> = (value: T) => void; | ||
/** | ||
@@ -35,3 +43,3 @@ * Add a computed property to an observable object | ||
* @export | ||
* @param {object} obj Observable object | ||
* @param {Record<string, any>} obj Observable object | ||
* @param {string} key Property to add | ||
@@ -41,3 +49,3 @@ * @param {() => any} getter Getter function | ||
*/ | ||
export declare function assignComputed<T = any>(obj: object, key: string, getter?: () => T, setter?: (value: T) => void): void; | ||
export declare function assignComputed<T = any>(obj: Record<string, any>, key: string, getter?: Getter<T>, setter?: Setter<T>): void; | ||
export declare function error(...args: Array<any>): void; | ||
@@ -47,1 +55,2 @@ export declare function warn(...args: Array<any>): void; | ||
export declare function info(...args: Array<any>): void; | ||
export {}; |
@@ -1,1 +0,1 @@ | ||
"use strict";Object.defineProperty(exports,"__esModule",{value:!0});var mobx=require("mobx"),DEFAULT_TYPE="__DEFAULT_TYPE__",META_FIELD="__META__";function mapItems(e,r){return e instanceof Array?e.map(function(e){return r(e)}):null===e?null:r(e)}function flatten(e){var r;return(r=[]).concat.apply(r,e)}function isFalsyArray(e){return e instanceof Array&&!e.every(Boolean)}function undefinedGetter(){}function defaultSetter(){throw new Error("The setter is not defined for this property")}function assignComputed(r,t,e,n){var o;void 0===e&&(e=undefinedGetter),void 0===n&&(n=defaultSetter);var a=Symbol.for("datx administration");r.hasOwnProperty(a)||Object.defineProperty(r,a,{configurable:!1,enumerable:!1,value:{}}),r[a]["get__"+t]=e,r[a]["set__"+t]=n,r.hasOwnProperty(t)||mobx.extendObservable(r,(o={},Object.defineProperty(o,t,{get:function(){return r[a]["get__"+t]()},enumerable:!0,configurable:!0}),Object.defineProperty(o,t,{set:function(e){n&&r[a]["set__"+t](e)},enumerable:!0,configurable:!0}),o))}function error(){for(var e=[],r=0;r<arguments.length;r++)e[r]=arguments[r];console.error.apply(console,["[datx error]"].concat(e))}function warn(){for(var e=[],r=0;r<arguments.length;r++)e[r]=arguments[r];"production"!==process.env.NODE_ENV&&"test"!==process.env.NODE_ENV&&console.warn.apply(console,["[datx warning]"].concat(e))}function deprecated(){for(var e=[],r=0;r<arguments.length;r++)e[r]=arguments[r];"production"!==process.env.NODE_ENV&&"test"!==process.env.NODE_ENV&&console.warn.apply(console,["[datx deprecated]"].concat(e))}function info(){for(var e=[],r=0;r<arguments.length;r++)e[r]=arguments[r];"production"!==process.env.NODE_ENV&&"test"!==process.env.NODE_ENV&&console.info.apply(console,["[datx info]"].concat(e))}exports.DEFAULT_TYPE=DEFAULT_TYPE,exports.META_FIELD=META_FIELD,exports.assignComputed=assignComputed,exports.deprecated=deprecated,exports.error=error,exports.flatten=flatten,exports.info=info,exports.isFalsyArray=isFalsyArray,exports.mapItems=mapItems,exports.warn=warn; | ||
"use strict";Object.defineProperty(exports,"__esModule",{value:!0});var e=require("mobx"),r=Symbol.for("datx administration"),t=function(){return(t=Object.assign||function(e){for(var r,t=1,n=arguments.length;t<n;t++)for(var o in r=arguments[t])Object.prototype.hasOwnProperty.call(r,o)&&(e[o]=r[o]);return e}).apply(this,arguments)};function n(){for(var e=0,r=0,t=arguments.length;r<t;r++)e+=arguments[r].length;var n=Array(e),o=0;for(r=0;r<t;r++)for(var a=arguments[r],s=0,c=a.length;s<c;s++,o++)n[o]=a[s];return n}function o(e,r,t){for(var n=t,o=e;o;)n=r(n,o),o=Object.getPrototypeOf(o);return n}function a(){}function s(){throw new Error("The setter is not defined for this property")}function c(t){return Object.prototype.hasOwnProperty.call(t,r)||Object.defineProperty(t,r,{configurable:!1,enumerable:!1,value:e.observable({},{},{deep:!1})}),t[r]}exports.DEFAULT_TYPE="__DEFAULT_TYPE__",exports.META_FIELD="__META__",exports.assignComputed=function(r,t,n,o){var c;void 0===n&&(n=a),void 0===o&&(o=s);var i=e.extendObservable({},(c={},Object.defineProperty(c,t,{get:function(){return n.call(r)},enumerable:!0,configurable:!0}),c));Object.defineProperty(r,t,{get:function(){return i[t]},set:function(e){o.call(r,e)},enumerable:!0,configurable:!0})},exports.deprecated=function(){for(var e=[],r=0;r<arguments.length;r++)e[r]=arguments[r];"production"!==process.env.NODE_ENV&&"test"!==process.env.NODE_ENV&&console.warn.apply(console,n(["[datx deprecated]"],e))},exports.error=function(){for(var e=[],r=0;r<arguments.length;r++)e[r]=arguments[r];console.error.apply(console,n(["[datx error]"],e))},exports.flatten=function(e){var r;return(r=[]).concat.apply(r,e)},exports.getMeta=function e(r,n,a,s,i){if(s)return o(r,(function(r,o){var a=e(o,n,i?{}:void 0);return i?t(t({},a),r):r||a}),i?{}:void 0)||a;var p=c(r);return void 0===p[n]?a:p[n]},exports.getMetaObj=c,exports.info=function(){for(var e=[],r=0;r<arguments.length;r++)e[r]=arguments[r];"production"!==process.env.NODE_ENV&&"test"!==process.env.NODE_ENV&&console.info.apply(console,n(["[datx info]"],e))},exports.isFalsyArray=function(r){return e.isArrayLike(r)&&!r.every(Boolean)},exports.mapItems=function(r,t){return e.isArrayLike(r)?r.map((function(e){return t(e)})):null===r?null:t(r)},exports.mergeMeta=function(e,r){var t=c(e);return Object.assign(t,r),t},exports.reducePrototypeChain=o,exports.setMeta=function(r,t,n){var o=c(r);e.set(o,t,n)},exports.warn=function(){for(var e=[],r=0;r<arguments.length;r++)e[r]=arguments[r];"production"!==process.env.NODE_ENV&&"test"!==process.env.NODE_ENV&&console.warn.apply(console,n(["[datx warning]"],e))}; |
export { DEFAULT_TYPE, META_FIELD } from './consts'; | ||
export { IDictionary } from './interfaces/IDictionary'; | ||
export { IRawModel } from './interfaces/IRawModel'; | ||
export { assignComputed, mapItems, flatten, isFalsyArray, error, warn, deprecated, info, } from './helpers'; | ||
export { assignComputed, mapItems, flatten, isFalsyArray, error, warn, deprecated, info, getMeta, setMeta, mergeMeta, reducePrototypeChain, getMetaObj, } from './helpers'; |
@@ -1,9 +0,52 @@ | ||
import { extendObservable } from 'mobx'; | ||
import { isArrayLike, observable, set, extendObservable } from 'mobx'; | ||
var DEFAULT_TYPE = '__DEFAULT_TYPE__'; | ||
var META_FIELD = '__META__'; // Note: Hardcoded in interfaces/IRawModel | ||
var DATX_META = Symbol.for('datx administration'); | ||
/*! ***************************************************************************** | ||
Copyright (c) Microsoft Corporation. All rights reserved. | ||
Licensed under the Apache License, Version 2.0 (the "License"); you may not use | ||
this file except in compliance with the License. You may obtain a copy of the | ||
License at http://www.apache.org/licenses/LICENSE-2.0 | ||
THIS CODE IS PROVIDED ON AN *AS IS* BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY | ||
KIND, EITHER EXPRESS OR IMPLIED, INCLUDING WITHOUT LIMITATION ANY IMPLIED | ||
WARRANTIES OR CONDITIONS OF TITLE, FITNESS FOR A PARTICULAR PURPOSE, | ||
MERCHANTABLITY OR NON-INFRINGEMENT. | ||
See the Apache Version 2.0 License for specific language governing permissions | ||
and limitations under the License. | ||
***************************************************************************** */ | ||
var __assign = function() { | ||
__assign = Object.assign || function __assign(t) { | ||
for (var s, i = 1, n = arguments.length; i < n; i++) { | ||
s = arguments[i]; | ||
for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p)) t[p] = s[p]; | ||
} | ||
return t; | ||
}; | ||
return __assign.apply(this, arguments); | ||
}; | ||
function __spreadArrays() { | ||
for (var s = 0, i = 0, il = arguments.length; i < il; i++) s += arguments[i].length; | ||
for (var r = Array(s), k = 0, i = 0; i < il; i++) | ||
for (var a = arguments[i], j = 0, jl = a.length; j < jl; j++, k++) | ||
r[k] = a[j]; | ||
return r; | ||
} | ||
function reducePrototypeChain(obj, reduceFn, initialValue) { | ||
var value = initialValue; | ||
var model = obj; | ||
while (model) { | ||
value = reduceFn(value, model); | ||
model = Object.getPrototypeOf(model); | ||
} | ||
return value; | ||
} | ||
function mapItems(data, fn) { | ||
if (data instanceof Array) { | ||
// tslint:disable-next-line:no-unnecessary-callback-wrapper | ||
if (isArrayLike(data)) { | ||
return data.map(function (item) { return fn(item); }); | ||
@@ -33,3 +76,3 @@ } | ||
function isFalsyArray(value) { | ||
return value instanceof Array && !value.every(Boolean); | ||
return isArrayLike(value) && !value.every(Boolean); | ||
} | ||
@@ -42,2 +85,32 @@ function undefinedGetter() { | ||
} | ||
function getMetaObj(obj) { | ||
if (!Object.prototype.hasOwnProperty.call(obj, DATX_META)) { | ||
Object.defineProperty(obj, DATX_META, { | ||
configurable: false, | ||
enumerable: false, | ||
value: observable({}, {}, { deep: false }), | ||
}); | ||
} | ||
// @ts-ignore https://github.com/microsoft/TypeScript/issues/1863 | ||
return obj[DATX_META]; | ||
} | ||
function setMeta(obj, key, value) { | ||
var meta = getMetaObj(obj); | ||
set(meta, key, value); | ||
} | ||
function getMeta(obj, key, defaultValue, includeChain, mergeChain) { | ||
if (includeChain) { | ||
return (reducePrototypeChain(obj, function (value, model) { | ||
var meta = getMeta(model, key, mergeChain ? {} : undefined); | ||
return mergeChain ? __assign(__assign({}, meta), value) : (value || meta); | ||
}, (mergeChain ? {} : undefined)) || defaultValue); | ||
} | ||
var meta = getMetaObj(obj); | ||
return meta[key] === undefined ? defaultValue : meta[key]; | ||
} | ||
function mergeMeta(obj, newMeta) { | ||
var meta = getMetaObj(obj); | ||
Object.assign(meta, newMeta); | ||
return meta; | ||
} | ||
/** | ||
@@ -47,3 +120,3 @@ * Add a computed property to an observable object | ||
* @export | ||
* @param {object} obj Observable object | ||
* @param {Record<string, any>} obj Observable object | ||
* @param {string} key Property to add | ||
@@ -54,35 +127,27 @@ * @param {() => any} getter Getter function | ||
function assignComputed(obj, key, getter, setter) { | ||
// if (isObservable(obj)) { | ||
// throw new Error(`[datx exception] This object shouldn't be an observable`); | ||
// } | ||
var _a; | ||
if (getter === void 0) { getter = undefinedGetter; } | ||
if (setter === void 0) { setter = defaultSetter; } | ||
var admin = Symbol.for('datx administration'); | ||
if (!obj.hasOwnProperty(admin)) { | ||
Object.defineProperty(obj, admin, { | ||
configurable: false, | ||
enumerable: false, | ||
value: {}, | ||
}); | ||
} | ||
obj[admin]["get__" + key] = getter; | ||
obj[admin]["set__" + key] = setter; | ||
if (!obj.hasOwnProperty(key)) { | ||
extendObservable(obj, (_a = {}, | ||
Object.defineProperty(_a, key, { | ||
get: function () { | ||
return obj[admin]["get__" + key](); | ||
}, | ||
enumerable: true, | ||
configurable: true | ||
}), | ||
Object.defineProperty(_a, key, { | ||
set: function (val) { | ||
if (setter) { | ||
obj[admin]["set__" + key](val); | ||
} | ||
}, | ||
enumerable: true, | ||
configurable: true | ||
}), | ||
_a)); | ||
} | ||
var computedObj = extendObservable({}, (_a = {}, | ||
Object.defineProperty(_a, key, { | ||
get: function () { | ||
return getter.call(obj); | ||
}, | ||
enumerable: true, | ||
configurable: true | ||
}), | ||
_a)); | ||
Object.defineProperty(obj, key, { | ||
get: function () { | ||
return computedObj[key]; | ||
}, | ||
set: function (val) { | ||
setter.call(obj, val); | ||
}, | ||
enumerable: true, | ||
configurable: true, | ||
}); | ||
} | ||
@@ -94,4 +159,4 @@ function error() { | ||
} | ||
// tslint:disable-next-line:no-console | ||
console.error.apply(console, ["[datx error]"].concat(args)); | ||
// eslint-disable-next-line no-console | ||
console.error.apply(console, __spreadArrays(["[datx error]"], args)); | ||
} | ||
@@ -106,4 +171,4 @@ function warn() { | ||
} | ||
// tslint:disable-next-line:no-console | ||
console.warn.apply(console, ["[datx warning]"].concat(args)); | ||
// eslint-disable-next-line no-console | ||
console.warn.apply(console, __spreadArrays(["[datx warning]"], args)); | ||
} | ||
@@ -118,4 +183,4 @@ function deprecated() { | ||
} | ||
// tslint:disable-next-line:no-console | ||
console.warn.apply(console, ["[datx deprecated]"].concat(args)); | ||
// eslint-disable-next-line no-console | ||
console.warn.apply(console, __spreadArrays(["[datx deprecated]"], args)); | ||
} | ||
@@ -130,6 +195,6 @@ function info() { | ||
} | ||
// tslint:disable-next-line:no-console | ||
console.info.apply(console, ["[datx info]"].concat(args)); | ||
// eslint-disable-next-line no-console | ||
console.info.apply(console, __spreadArrays(["[datx info]"], args)); | ||
} | ||
export { DEFAULT_TYPE, META_FIELD, assignComputed, deprecated, error, flatten, info, isFalsyArray, mapItems, warn }; | ||
export { DEFAULT_TYPE, META_FIELD, assignComputed, deprecated, error, flatten, getMeta, getMetaObj, info, isFalsyArray, mapItems, mergeMeta, reducePrototypeChain, setMeta, warn }; |
@@ -1,4 +0,3 @@ | ||
import { IDictionary } from './IDictionary'; | ||
export interface IRawModel extends IDictionary { | ||
__META__?: IDictionary; | ||
export interface IRawModel extends Record<string, any> { | ||
__META__?: Record<string, any>; | ||
} |
{ | ||
"name": "datx-utils", | ||
"version": "1.0.0", | ||
"version": "2.0.0-beta.0", | ||
"description": "DatX lib utils for mixins", | ||
@@ -24,13 +24,15 @@ "main": "dist/index.cjs.js", | ||
"devDependencies": { | ||
"@infinumjs/tslint-config": "^2.0.0-beta.6", | ||
"@types/jest": "^24.0.11", | ||
"@types/node": "^11.13.4", | ||
"jest": "^24.7.1", | ||
"mobx": "^5.9.4", | ||
"rollup": "^1.10.0", | ||
"rollup-plugin-typescript2": "^0.20.1", | ||
"rollup-plugin-uglify": "^6.0.2", | ||
"ts-jest": "^24.0.2", | ||
"tslint": "^5.15.0", | ||
"typescript": "^3.4.3" | ||
"@rollup/plugin-commonjs": "^11.0.2", | ||
"@types/jest": "^25.1.1", | ||
"@types/node": "^13.7.0", | ||
"eslint": "^6.8.0", | ||
"jest": "^25.1.0", | ||
"mobx": "^5.15.4", | ||
"rollup": "^1.31.0", | ||
"rollup-plugin-exclude-dependencies-from-bundle": "^1.1.6", | ||
"rollup-plugin-node-resolve": "^5.2.0", | ||
"rollup-plugin-terser": "^5.2.0", | ||
"rollup-plugin-typescript2": "^0.25.3", | ||
"ts-jest": "^25.1.0", | ||
"typescript": "^3.7.5" | ||
}, | ||
@@ -43,3 +45,2 @@ "peerDependencies": { | ||
"watch": "jest --watch --coverage", | ||
"lint": "tslint --project .", | ||
"prepublish": "npm run build", | ||
@@ -68,3 +69,3 @@ "build": "rollup -c" | ||
}, | ||
"gitHead": "16f55b33bb5d4318de4867720e83571363bf1437" | ||
"dependencies": {} | ||
} |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
No README
QualityPackage does not have a README. This may indicate a failed publish or a low quality package.
Found 1 instance in 1 package
Mixed license
License(Experimental) Package contains multiple licenses.
Found 1 instance in 1 package
No v1
QualityPackage is not semver >=1. This means it is not stable and does not support ^ ranges.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
13893
255
13
7
1
2
2
0