@karmaniverous/jsonmap 0.2.7 → 0.3.0

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/README.md CHANGED
@@ -122,19 +122,20 @@ JsonMap class to apply transformations to a JSON object
122
122
  **Kind**: global class
123
123
 
124
124
  * [JsonMap](#JsonMap)
125
- * [new JsonMap([map], [lib])](#new_JsonMap_new)
125
+ * [new JsonMap([map], [lib], [ignore])](#new_JsonMap_new)
126
126
  * [.transform(input)](#JsonMap+transform) ⇒ <code>object</code>
127
127
 
128
128
  <a name="new_JsonMap_new"></a>
129
129
 
130
- ### new JsonMap([map], [lib])
130
+ ### new JsonMap([map], [lib], [ignore])
131
131
  Creates an instance of JsonMap.
132
132
 
133
133
 
134
- | Param | Type | Description |
135
- | --- | --- | --- |
136
- | [map] | <code>object</code> | The data mapping configuration. |
137
- | [lib] | <code>object</code> | A collection of function libraries. |
134
+ | Param | Type | Default | Description |
135
+ | --- | --- | --- | --- |
136
+ | [map] | <code>object</code> | | The data mapping configuration. |
137
+ | [lib] | <code>object</code> | | A collection of function libraries. |
138
+ | [ignore] | <code>string</code> | <code>&quot;^\\$&quot;</code> | Regex pattern of keys to ignore. Defaults to '^\\$'. |
138
139
 
139
140
  <a name="JsonMap+transform"></a>
140
141
 
@@ -8,17 +8,16 @@ var _lodash = _interopRequireDefault(require("lodash.castarray"));
8
8
  var _lodash2 = _interopRequireDefault(require("lodash.clonedeepwith"));
9
9
  var _lodash3 = _interopRequireDefault(require("lodash.get"));
10
10
  var _lodash4 = _interopRequireDefault(require("lodash.invoke"));
11
- var _lodash5 = _interopRequireDefault(require("lodash.isarray"));
12
- var _lodash6 = _interopRequireDefault(require("lodash.isarraylikeobject"));
13
- var _lodash7 = _interopRequireDefault(require("lodash.isobject"));
14
- var _lodash8 = _interopRequireDefault(require("lodash.isplainobject"));
15
- var _lodash9 = _interopRequireDefault(require("lodash.isstring"));
16
- var _lodash10 = _interopRequireDefault(require("lodash.isundefined"));
17
- var _lodash11 = _interopRequireDefault(require("lodash.mapvalues"));
18
- var _lodash12 = _interopRequireDefault(require("lodash.pickby"));
19
- var _lodash13 = _interopRequireDefault(require("lodash.set"));
20
- var _lodash14 = _interopRequireDefault(require("lodash.size"));
21
- var _lodash15 = _interopRequireDefault(require("lodash.sortby"));
11
+ var _lodash5 = _interopRequireDefault(require("lodash.isarraylikeobject"));
12
+ var _lodash6 = _interopRequireDefault(require("lodash.isobject"));
13
+ var _lodash7 = _interopRequireDefault(require("lodash.isplainobject"));
14
+ var _lodash8 = _interopRequireDefault(require("lodash.isstring"));
15
+ var _lodash9 = _interopRequireDefault(require("lodash.isundefined"));
16
+ var _lodash10 = _interopRequireDefault(require("lodash.mapvalues"));
17
+ var _lodash11 = _interopRequireDefault(require("lodash.pickby"));
18
+ var _lodash12 = _interopRequireDefault(require("lodash.set"));
19
+ var _lodash13 = _interopRequireDefault(require("lodash.size"));
20
+ var _lodash14 = _interopRequireDefault(require("lodash.sortby"));
22
21
  var _nanoid = require("nanoid");
23
22
  function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
24
23
  // npm imports
@@ -32,12 +31,12 @@ const getJsonFns = () => {
32
31
  return 'CIRCULAR REFERENCE';
33
32
  }
34
33
  seen.add(value);
35
- if (!(0, _lodash6.default)(value)) return Object.getOwnPropertyNames(value).reduce((v, p) => ({
34
+ if (!(0, _lodash5.default)(value)) return Object.getOwnPropertyNames(value).reduce((v, p) => ({
36
35
  ...v,
37
36
  [p]: value[p]
38
37
  }), {});
39
38
  }
40
- return (0, _lodash10.default)(value) ? undefinedToken : value;
39
+ return (0, _lodash9.default)(value) ? undefinedToken : value;
41
40
  };
42
41
  const reviver = (key, value) => value === undefinedToken ? undefined : value;
43
42
  return {
@@ -55,12 +54,15 @@ class JsonMap {
55
54
  *
56
55
  * @param {object} [map] - The data mapping configuration.
57
56
  * @param {object} [lib] - A collection of function libraries.
57
+ * @param {string} [ignore] - Regex pattern of keys to ignore. Defaults to '^\\$'.
58
58
  */
59
59
  constructor() {
60
60
  let map = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : {};
61
61
  let lib = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : {};
62
+ let ignore = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : '^\\$';
62
63
  this.map = map;
63
64
  this.lib = lib;
65
+ this.ignore = new RegExp(ignore);
64
66
  }
65
67
 
66
68
  /**
@@ -81,8 +83,8 @@ class JsonMap {
81
83
  } = getJsonFns();
82
84
  const result = JSON.parse(JSON.stringify(await this.#transform(this.map, this.input, this.output), replacer), reviver);
83
85
 
84
- // Recursively eliminate non-string keys & string keys starting with $.
85
- const deep = value => (0, _lodash8.default)(value) ? (0, _lodash11.default)((0, _lodash12.default)(value, (v, k) => /^[^$]/.test(k)), value => (0, _lodash2.default)(value, deep)) : undefined;
86
+ // Recursively eliminate non-string keys & string keys starting with $ and not in ignoreExclusions.
87
+ const deep = value => (0, _lodash7.default)(value) ? (0, _lodash10.default)((0, _lodash11.default)(value, (v, k) => !this.ignore.test(k)), value => (0, _lodash2.default)(value, deep)) : undefined;
86
88
  return (0, _lodash2.default)(result, deep);
87
89
  }
88
90
 
@@ -101,7 +103,7 @@ class JsonMap {
101
103
  // console.debug('#transform params:\n', { node, input, output, path });
102
104
 
103
105
  // Checks if the current node is an object and has a '$' key
104
- if ((0, _lodash8.default)(node) && (0, _lodash14.default)(node) === 1 && '$' in node) {
106
+ if ((0, _lodash7.default)(node) && (0, _lodash13.default)(node) === 1 && '$' in node) {
105
107
  // Retrieves the transformations to be applied (can be an array or a single object)
106
108
  const transformations = (0, _lodash.default)(node['$']);
107
109
  // console.debug('transformations:\n', transformations);
@@ -139,19 +141,19 @@ class JsonMap {
139
141
  }
140
142
 
141
143
  // Sets the output at the specified path to the last result of the transformations & returns.
142
- (0, _lodash13.default)(output, path, results[0]);
144
+ (0, _lodash12.default)(output, path, results[0]);
143
145
  // console.debug('updated output:\n', output);
144
146
 
145
147
  return results[0];
146
148
  }
147
149
 
148
150
  // Checks if the current node is an object
149
- if ((0, _lodash7.default)(node)) {
151
+ if ((0, _lodash6.default)(node)) {
150
152
  // Creates an empty array or object based on whether the current node is an array or not
151
- const transformedNode = (0, _lodash5.default)(node) ? [] : {};
153
+ const transformedNode = Array.isArray(node) ? [] : {};
152
154
 
153
155
  // Iterates over each key-value pair in the current node in ascending order by key
154
- for (const [key, value] of (0, _lodash15.default)(Object.entries(node), _ref => {
156
+ for (const [key, value] of (0, _lodash14.default)(Object.entries(node), _ref => {
155
157
  let [key] = _ref;
156
158
  return key;
157
159
  })) {
@@ -164,12 +166,12 @@ class JsonMap {
164
166
  }
165
167
 
166
168
  // Sets the output at the specified path to the transformedNode & returnsd.
167
- (0, _lodash13.default)(output, path, transformedNode);
169
+ (0, _lodash12.default)(output, path, transformedNode);
168
170
  return transformedNode;
169
171
  }
170
172
 
171
173
  // Sets the output at the specified path to the current node & returns.
172
- (0, _lodash13.default)(output, path, node);
174
+ (0, _lodash12.default)(output, path, node);
173
175
  return node;
174
176
  }
175
177
 
@@ -190,7 +192,7 @@ class JsonMap {
190
192
  */
191
193
  #resolvePath(path, results) {
192
194
  // If the path is not a string, return it as is
193
- if (!(0, _lodash9.default)(path)) {
195
+ if (!(0, _lodash8.default)(path)) {
194
196
  return {
195
197
  path
196
198
  };
@@ -3,7 +3,6 @@ import castArray from 'lodash.castarray';
3
3
  import cloneDeepWith from 'lodash.clonedeepwith';
4
4
  import get from 'lodash.get';
5
5
  import invoke from 'lodash.invoke';
6
- import isArray from 'lodash.isarray';
7
6
  import isArrayLikeObject from 'lodash.isarraylikeobject';
8
7
  import isObject from 'lodash.isobject';
9
8
  import isPlainObject from 'lodash.isplainobject';
@@ -51,10 +50,12 @@ class JsonMap {
51
50
  *
52
51
  * @param {object} [map] - The data mapping configuration.
53
52
  * @param {object} [lib] - A collection of function libraries.
53
+ * @param {string} [ignore] - Regex pattern of keys to ignore. Defaults to '^\\$'.
54
54
  */
55
- constructor(map = {}, lib = {}) {
55
+ constructor(map = {}, lib = {}, ignore = '^\\$') {
56
56
  this.map = map;
57
57
  this.lib = lib;
58
+ this.ignore = new RegExp(ignore);
58
59
  }
59
60
 
60
61
  /**
@@ -78,11 +79,11 @@ class JsonMap {
78
79
  reviver
79
80
  );
80
81
 
81
- // Recursively eliminate non-string keys & string keys starting with $.
82
+ // Recursively eliminate non-string keys & string keys starting with $ and not in ignoreExclusions.
82
83
  const deep = (value) =>
83
84
  isPlainObject(value)
84
85
  ? mapValues(
85
- pickBy(value, (v, k) => /^[^$]/.test(k)),
86
+ pickBy(value, (v, k) => !this.ignore.test(k)),
86
87
  (value) => cloneDeepWith(value, deep)
87
88
  )
88
89
  : undefined;
@@ -155,7 +156,7 @@ class JsonMap {
155
156
  // Checks if the current node is an object
156
157
  if (isObject(node)) {
157
158
  // Creates an empty array or object based on whether the current node is an array or not
158
- const transformedNode = isArray(node) ? [] : {};
159
+ const transformedNode = Array.isArray(node) ? [] : {};
159
160
 
160
161
  // Iterates over each key-value pair in the current node in ascending order by key
161
162
  for (const [key, value] of sortBy(Object.entries(node), ([key]) => key)) {
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@karmaniverous/jsonmap",
3
- "version": "0.2.7",
3
+ "version": "0.3.0",
4
4
  "publishConfig": {
5
5
  "access": "public"
6
6
  },