json-storage-formatter 1.0.7 → 1.0.9
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/lib/bundle.js +1 -1
- package/lib/json-storage-formatter.d.ts +35 -9
- package/package.json +1 -1
package/lib/bundle.js
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
!function(r,t){"object"==typeof exports&&"object"==typeof module?module.exports=t():"function"==typeof define&&define.amd?define([],t):"object"==typeof exports?exports["json-storage-formatter"]=t():r["json-storage-formatter"]=t()}(this,(()=>(()=>{"use strict";var r={991:(r,t,e)=>{var n=Object.create?function(r,t,e,n){void 0===n&&(n=e);var o=Object.getOwnPropertyDescriptor(t,e);o&&!("get"in o?!t.__esModule:o.writable||o.configurable)||(o={enumerable:!0,get:function(){return t[e]}}),Object.defineProperty(r,n,o)}:function(r,t,e,n){void 0===n&&(n=e),r[n]=t[e]};Object.defineProperty(t,"__esModule",{value:!0}),function(r,t){for(var e in r)"default"===e||Object.prototype.hasOwnProperty.call(t,e)||n(t,r,e)}(e(729),t)},729:(r,t)=>{function e(r
|
|
1
|
+
!function(r,t){"object"==typeof exports&&"object"==typeof module?module.exports=t():"function"==typeof define&&define.amd?define([],t):"object"==typeof exports?exports["json-storage-formatter"]=t():r["json-storage-formatter"]=t()}(this,(()=>(()=>{"use strict";var r={991:(r,t,e)=>{var n=Object.create?function(r,t,e,n){void 0===n&&(n=e);var o=Object.getOwnPropertyDescriptor(t,e);o&&!("get"in o?!t.__esModule:o.writable||o.configurable)||(o={enumerable:!0,get:function(){return t[e]}}),Object.defineProperty(r,n,o)}:function(r,t,e,n){void 0===n&&(n=e),r[n]=t[e]};Object.defineProperty(t,"__esModule",{value:!0}),function(r,t){for(var e in r)"default"===e||Object.prototype.hasOwnProperty.call(t,e)||n(t,r,e)}(e(729),t)},729:(r,t)=>{function e(r){return e="function"==typeof Symbol&&"symbol"==typeof Symbol.iterator?function(r){return typeof r}:function(r){return r&&"function"==typeof Symbol&&r.constructor===Symbol&&r!==Symbol.prototype?"symbol":typeof r},e(r)}function n(r,t,n){return(t=function(r){var t=function(r,t){if("object"!==e(r)||null===r)return r;var n=r[Symbol.toPrimitive];if(void 0!==n){var o=n.call(r,"string");if("object"!==e(o))return o;throw new TypeError("@@toPrimitive must return a primitive value.")}return String(r)}(r);return"symbol"===e(t)?t:String(t)}(t))in r?Object.defineProperty(r,t,{value:n,enumerable:!0,configurable:!0,writable:!0}):r[t]=n,r}function o(r,t){if(r){if("string"==typeof r)return i(r,t);var e=Object.prototype.toString.call(r).slice(8,-1);return"Object"===e&&r.constructor&&(e=r.constructor.name),"Map"===e||"Set"===e?Array.from(r):"Arguments"===e||/^(?:Ui|I)nt(?:8|16|32)(?:Clamped)?Array$/.test(e)?i(r,t):void 0}}function i(r,t){(null==t||t>r.length)&&(t=r.length);for(var e=0,n=new Array(t);e<t;e++)n[e]=r[e];return n}Object.defineProperty(t,"__esModule",{value:!0}),t.formatToStore=t.formatFromStore=t.isPrimitive=t.isFunction=t.isRegex=t.isDate=t.isString=t.isBoolean=t.isNumber=t.isNil=t.clone=void 0,t.clone=function(r){var e,u=(arguments.length>1&&void 0!==arguments[1]?arguments[1]:{}).shallow;if((0,t.isPrimitive)(r)||(0,t.isDate)(r))return r;if(Array.isArray(r))return u?function(r){if(Array.isArray(r))return i(r)}(e=r)||function(r){if("undefined"!=typeof Symbol&&null!=r[Symbol.iterator]||null!=r["@@iterator"])return Array.from(r)}(e)||o(e)||function(){throw new TypeError("Invalid attempt to spread non-iterable instance.\nIn order to be iterable, non-array objects must have a [Symbol.iterator]() method.")}():r.map((function(r){return(0,t.clone)(r)}));if(r instanceof Map){var a=Array.from(r.entries());return u?new Map(a):new Map(a.map((function(r){return(0,t.clone)(r)})))}if(r instanceof Set){var f=Array.from(r.values());return u?new Set(f):new Set(f.map((function(r){return(0,t.clone)(r)})))}return r instanceof RegExp?new RegExp(r.toString()):(0,t.isFunction)(r)?u?r:Object.create(r):u?Object.assign({},r):r instanceof Error?new Error(r.message):Object.keys(r).reduce((function(e,o){var i=r[o];return Object.assign(Object.assign({},e),n({},o,(0,t.clone)(i)))}),{})},t.isNil=function(r){return null==r},t.isNumber=function(r){return"number"==typeof r},t.isBoolean=function(r){return"boolean"==typeof r},t.isString=function(r){return"string"==typeof r},t.isDate=function(r){return r instanceof Date},t.isRegex=function(r){return r instanceof RegExp},t.isFunction=function(r){return"function"==typeof r||r instanceof Function},t.isPrimitive=function(r){return(0,t.isNil)(r)||(0,t.isNumber)(r)||(0,t.isBoolean)(r)||(0,t.isString)(r)||"symbol"===e(r)},t.formatFromStore=function(r){return function(r){var e,i;if((0,t.isPrimitive)(r))return r;if("date"===(null==r?void 0:r.$t))return new Date(r.$v);if("map"===(null==r?void 0:r.$t)){var u=(null!==(e=r.$v)&&void 0!==e?e:[]).map((function(r){var e,n,i=(n=2,function(r){if(Array.isArray(r))return r}(e=r)||function(r,t){var e=null==r?null:"undefined"!=typeof Symbol&&r[Symbol.iterator]||r["@@iterator"];if(null!=e){var n,o,i,u,a=[],f=!0,c=!1;try{if(i=(e=e.call(r)).next,0===t){if(Object(e)!==e)return;f=!1}else for(;!(f=(n=i.call(e)).done)&&(a.push(n.value),a.length!==t);f=!0);}catch(r){c=!0,o=r}finally{try{if(!f&&null!=e.return&&(u=e.return(),Object(u)!==u))return}finally{if(c)throw o}}return a}}(e,n)||o(e,n)||function(){throw new TypeError("Invalid attempt to destructure non-iterable instance.\nIn order to be iterable, non-array objects must have a [Symbol.iterator]() method.")}()),u=i[0],a=i[1];return[u,(0,t.formatFromStore)(a)]}));return new Map(u)}if("set"===(null==r?void 0:r.$t)){var a=null!==(i=r.$v)&&void 0!==i?i:[].map((function(r){return(0,t.formatFromStore)(r)}));return new Set(a)}return"regex"===(null==r?void 0:r.$t)?new RegExp(r.$v):"error"===(null==r?void 0:r.$t)?new Error(r.$v):Array.isArray(r)?r.map((function(r){return(0,t.formatFromStore)(r)})):"function"===(null==r?void 0:r.$t)?Function("(".concat(r.$v,")(...arguments)")):Object.keys(r).reduce((function(e,o){var i=r[o];return Object.assign(Object.assign({},e),n({},o,(0,t.formatFromStore)(i)))}),{})}((arguments.length>1&&void 0!==arguments[1]?arguments[1]:{}).jsonParse?JSON.parse(r):(0,t.clone)(r))},t.formatToStore=function(r){var o=arguments.length>1&&void 0!==arguments[1]?arguments[1]:{stringify:!1},i=o.stringify,u=o.validator,a=o.excludeTypes,f=o.excludeKeys,c=new Set(null!=a?a:[]),s=new Set(null!=f?f:[]),l=c.size||s.size,m=null!=u?u:function(r){var t=r.key,n=r.value;if(!l)return!0;var o=s.has(t),i=c.has(e(n));return!o&&!i},v=function r(e){if((0,t.isPrimitive)(e))return e;if(Array.isArray(e))return e.map((function(t){return r(t)}));if(e instanceof Map)return{$t:"map",$v:Array.from(e.entries()).map((function(t){return r(t)}))};if(e instanceof Set)return{$t:"set",$v:Array.from(e.values()).map((function(t){return r(t)}))};if((0,t.isDate)(e))return{$t:"date",$v:e.toISOString()};if((0,t.isRegex)(e))return{$t:"regex",$v:e.toString()};if((0,t.isFunction)(e)){var o;try{o={$t:"function",$v:e.toString()}}catch(r){o={$t:"error",$v:"Error: Could not serialize function"}}return o}return e instanceof Error?{$t:"error",$v:e.message}:Object.keys(e).reduce((function(t,o){var i=e[o],u=r(i);return m({obj:e,key:o,value:u})?Object.assign(Object.assign({},t),n({},o,r(i))):t}),{})}((0,t.clone)(r));return i?JSON.stringify(v):v}}},t={};return function e(n){var o=t[n];if(void 0!==o)return o.exports;var i=t[n]={exports:{}};return r[n](i,i.exports,e),i.exports}(991)})()));
|
|
@@ -1,14 +1,19 @@
|
|
|
1
|
-
export type
|
|
2
|
-
$t?: 'map' | 'set' | 'date' | 'regex' | 'error';
|
|
1
|
+
export type IValueWithMetaData = {
|
|
2
|
+
$t?: 'map' | 'set' | 'date' | 'regex' | 'error' | 'function';
|
|
3
3
|
$v?: unknown;
|
|
4
4
|
};
|
|
5
5
|
/**
|
|
6
6
|
* Deep clone an object, it also suppors Map, Set, Arrays
|
|
7
|
-
* @param
|
|
7
|
+
* @param value
|
|
8
8
|
* @returns
|
|
9
9
|
* A deep clone of the object
|
|
10
10
|
* */
|
|
11
|
-
export declare const clone: <T>(
|
|
11
|
+
export declare const clone: <T>(value: T, { shallow, }?: {
|
|
12
|
+
/**
|
|
13
|
+
* If true, it will only clone the first level of the object
|
|
14
|
+
*/
|
|
15
|
+
shallow?: boolean;
|
|
16
|
+
}) => T;
|
|
12
17
|
/**
|
|
13
18
|
* Check if a value is null or undefined
|
|
14
19
|
* @param value
|
|
@@ -54,6 +59,12 @@ export declare const isDate: (value: unknown) => boolean;
|
|
|
54
59
|
* @returns true if the value is a RegExp, false otherwise
|
|
55
60
|
* */
|
|
56
61
|
export declare const isRegex: (value: unknown) => boolean;
|
|
62
|
+
/**
|
|
63
|
+
* Check if a value is a function
|
|
64
|
+
* @param value The value to check
|
|
65
|
+
* @returns true if the value is a function, false otherwise
|
|
66
|
+
* */
|
|
67
|
+
export declare const isFunction: (value: unknown) => boolean;
|
|
57
68
|
/**
|
|
58
69
|
* Check if a value is a primitive
|
|
59
70
|
* @param value
|
|
@@ -67,17 +78,32 @@ export declare const isPrimitive: (value: unknown) => boolean;
|
|
|
67
78
|
* Format an value with possible metadata to his original form, it also supports Map, Set, Arrays
|
|
68
79
|
* @param value
|
|
69
80
|
* @returns
|
|
70
|
-
*
|
|
81
|
+
* Original form of the value
|
|
71
82
|
*/
|
|
72
|
-
export declare const formatFromStore: <T = unknown>(value: unknown
|
|
83
|
+
export declare const formatFromStore: <T = unknown>(value: unknown, { jsonParse, }?: {
|
|
84
|
+
/** If the value should be parsed from json before formatting */
|
|
85
|
+
jsonParse?: boolean;
|
|
86
|
+
}) => T;
|
|
87
|
+
export type TPrimitives = 'string' | 'number' | 'bigint' | 'boolean' | 'symbol' | 'undefined' | 'function' | 'object';
|
|
73
88
|
/**
|
|
74
89
|
* Add metadata to a value to store it as json, it also supports Map, Set, Arrays,
|
|
75
|
-
* Returns a new object
|
|
90
|
+
* Returns a new object which is a clone of the original object with metadata
|
|
76
91
|
* @template {TValue} The type of the value to format
|
|
77
92
|
* @template {TStringify} If the value should be stringified
|
|
78
93
|
* @param {TValue} value The value to format
|
|
79
94
|
* @param {{ stringify: TStringify }} { stringify: boolean } If the value should be stringified
|
|
80
95
|
*/
|
|
81
|
-
export declare const formatToStore: <TValue, TStringify extends boolean = false>(value: TValue, { stringify }?: {
|
|
82
|
-
stringify
|
|
96
|
+
export declare const formatToStore: <TValue, TStringify extends boolean = false>(value: TValue, { stringify, validator, excludeTypes, excludeKeys, }?: {
|
|
97
|
+
stringify?: TStringify;
|
|
98
|
+
excludeTypes?: TPrimitives[] | Set<TPrimitives>;
|
|
99
|
+
excludeKeys?: string[] | Set<string>;
|
|
100
|
+
/**
|
|
101
|
+
* Returns true if the value should be included in the stringified version,
|
|
102
|
+
* if provided it will override the default validator and the excludesTypes and excludeKeys
|
|
103
|
+
*/
|
|
104
|
+
validator?: ({ obj, key, value, }: {
|
|
105
|
+
obj: unknown;
|
|
106
|
+
key: string;
|
|
107
|
+
value: any;
|
|
108
|
+
}) => boolean | undefined;
|
|
83
109
|
}) => TStringify extends true ? string : unknown;
|