@lumjs/core 1.38.8 → 1.39.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/lib/obj/df.js CHANGED
@@ -1,6 +1,6 @@
1
1
  "use strict";
2
2
 
3
- const {B,F,S,doesDescriptor} = require('../types/basics');
3
+ const {doesDescriptor} = require('../types/basics');
4
4
  const {needObj,needType} = require('../types/needs');
5
5
  const TYPES = require('../types/typelist');
6
6
 
@@ -67,8 +67,8 @@ function descriptor(desc, opts)
67
67
 
68
68
  for (const dp of dps)
69
69
  {
70
- if (typeof opts[dp] === B
71
- && (force.includes(dp) || typeof desc[dp] !== B))
70
+ if (typeof opts[dp] === 'boolean'
71
+ && (force.includes(dp) || typeof desc[dp] !== 'boolean'))
72
72
  {
73
73
  desc[dp] = opts[dp];
74
74
  }
@@ -122,11 +122,11 @@ function df(obj, name, value, opts={})
122
122
  needObj(obj, {log, fun: true});
123
123
  needType(TYPES.PROP, name, {log});
124
124
 
125
- if (typeof opts === B)
125
+ if (typeof opts === 'boolean')
126
126
  {
127
127
  opts = {useDesc: opts};
128
128
  }
129
- else if (typeof opts === F && typeof value === F)
129
+ else if (typeof opts === 'function' && typeof value === 'function')
130
130
  {
131
131
  value = {get: value, set: opts};
132
132
  opts = {useDesc: true};
@@ -220,7 +220,7 @@ function dfor(obj, opts={}, prev=null)
220
220
  {
221
221
  const fnOne = function (name, value, setter)
222
222
  {
223
- if (typeof setter === F && typeof value === F)
223
+ if (typeof setter === 'function' && typeof value === 'function')
224
224
  {
225
225
  value = {get: value, set: setter}
226
226
  opts = clone(opts, {useDesc: true});
@@ -334,7 +334,7 @@ function lazy(target, name, initfunc, opts={})
334
334
  let log = {target, name, initfunc, opts};
335
335
  needObj(target, {log, fun: true});
336
336
  needType(TYPES.PROP, name, {log});
337
- needType(F, initfunc, {log});
337
+ needType('function', initfunc, {log});
338
338
  needObj(opts, {log});
339
339
 
340
340
  // The `this` object for the functions.
@@ -367,7 +367,7 @@ function lazy(target, name, initfunc, opts={})
367
367
  }
368
368
  else if (doesDescriptor(value))
369
369
  { // A descriptor was returned, extract the real value.
370
- if (typeof value.get === F)
370
+ if (typeof value.get === 'function')
371
371
  {
372
372
  return value.get();
373
373
  }
@@ -387,7 +387,7 @@ function lazy(target, name, initfunc, opts={})
387
387
  return defval(initfunc.call(ctx,ctx));
388
388
  }
389
389
 
390
- if (typeof opts.set === F)
390
+ if (typeof opts.set === 'function')
391
391
  { // We want to assign a lazy setter as well.
392
392
  desc.set = function(value)
393
393
  {
@@ -1,6 +1,6 @@
1
1
  "use strict";
2
2
 
3
- const {isComplex,isObj} = require('../types');
3
+ const {isComplex,isObj} = require('../types/basics');
4
4
 
5
5
  /**
6
6
  * Get a property descriptor.
package/lib/obj/index.js CHANGED
@@ -5,20 +5,15 @@
5
5
 
6
6
  const {apply,ApplyInfo,ApplyOpts,ApplyWith} = require('./apply');
7
7
  const assignd = require('./assignd');
8
- const {copyAll,duplicateOne,duplicateAll} = require('./copyall');
9
- const copyProps = require('./copyprops');
10
- const {CLONE,clone,addClone,cloneIfLocked} = require('./clone');
11
8
  const {df,dfa,dfor,lazy} = require('./df');
12
9
  const {flip, flipKeyVal, flipMap} = require('./flip');
13
10
  const {getMethods,signatureOf,MethodFilter} = require('./getmethods');
14
11
  const getProperty = require('./getproperty');
15
12
  const getPrototypesOf = require('./getprotos');
16
- const {lock,addLock} = require('./lock');
17
- const {mergeNested,syncNested} = require('./merge');
18
13
  const ns = require('./ns');
19
- const cp = require('./cp');
20
14
  const unlocked = require('./unlocked');
21
15
  const ownCount = require('./owncount');
16
+ const {getOwnKeys,isEmptyObject} = require('./keys');
22
17
 
23
18
  const
24
19
  {
@@ -26,6 +21,11 @@ const
26
21
  getNamespace,setNamespace,nsFactory,
27
22
  } = ns;
28
23
 
24
+ // Deprecated functions; TODO: remove in 2.0
25
+ const NSO = '@lumjs/core/obj';
26
+ const { deprecateAll } = require('../meta');
27
+ const cp = deprecateAll(require('./cp'), NSO+'/cp');
28
+
29
29
  /**
30
30
  * Create a new object with a `null` prototype.
31
31
  *
@@ -43,13 +43,17 @@ const
43
43
  const discrete = (props) => Object.create(null, props);
44
44
 
45
45
  // TODO: reorder the following alphabetically, for sanity sake...
46
- module.exports =
46
+ exports = module.exports =
47
47
  {
48
- assignd, cp, CLONE, clone, addClone, cloneIfLocked, lock, addLock,
49
- df, dfa, dfor, lazy, discrete,
50
- mergeNested, syncNested, copyProps, copyAll, ns, nsFactory,
51
- getObjectPath, setObjectPath, delObjectPath, getNamespace, setNamespace,
52
- getProperty, duplicateAll, duplicateOne, getMethods, signatureOf,
53
- MethodFilter, flip, flipKeyVal, flipMap, unlocked, getPrototypesOf,
54
- apply, ApplyInfo, ApplyOpts, ApplyWith, ownCount,
48
+ cp, // TODO: remove this line in 2.0
49
+
50
+ apply, ApplyInfo, ApplyOpts, ApplyWith, assignd,
51
+ delObjectPath, df, dfa, dfor, discrete, flip, flipKeyVal, flipMap,
52
+ getMethods, getNamespace, getObjectPath, getOwnKeys, getProperty,
53
+ getPrototypesOf, isEmptyObject, lazy, MethodFilter, ns, nsFactory,
54
+ ownCount, setNamespace, setObjectPath, signatureOf, unlocked,
55
55
  }
56
+
57
+ // Deprecated collections; TODO: remove in 2.0
58
+ deprecateAll(require('@lumjs/cp/fun'), NSO, exports);
59
+ deprecateAll(require('@lumjs/cp/util'), NSO, exports);
@@ -0,0 +1,55 @@
1
+ 'use strict';
2
+
3
+ /**
4
+ * Get both string and symbol keys from an object.
5
+ * @param {object} obj - Target object.
6
+ * @returns {(string|symbol)[]}
7
+ */
8
+ const getOwnKeys = obj => [
9
+ ...(Object.getOwnPropertyNames(obj)),
10
+ ...(Object.getOwnPropertySymbols(obj)),
11
+ ];
12
+
13
+ /**
14
+ * See if an object has its own properties.
15
+ */
16
+ const isEmptyObject = obj => getOwnKeys(obj).length === 0;
17
+
18
+ /**
19
+ * Get the name of a property key.
20
+ * @param {(string|symbol)} key - Property key.
21
+ *
22
+ * If this is a string it will be returned as-is.
23
+ * If this is a symbol then how it is formatted depends
24
+ * on the options specified.
25
+ *
26
+ * @param {object} [opts] Options for handling symbol property keys.
27
+ * @param {string} [opts.prefix] Prefix character; default: `[`.
28
+ * @param {string} [opts.suffix] Suffix character; default: `]`.
29
+ * @param {boolean} [opts.toStr=false] Use toString method?
30
+ *
31
+ * If this is false (default), then the return value will be the
32
+ * `key.description` value with the prefix and suffix values applied.
33
+ *
34
+ * If this is true then the prefix/suffix options are ignored,
35
+ * and `key.toString()` will be used as the return value.
36
+ *
37
+ * @returns {string}
38
+ */
39
+ function keyName(key, opts={}) {
40
+ if (typeof key === 'string') return key;
41
+ if (typeof key === 'symbol') {
42
+ if (opts.toStr) {
43
+ return key.toString();
44
+ }
45
+
46
+ let pf = opts.prefix ?? '[';
47
+ let sf = opts.suffix ?? ']';
48
+ return pf+key.description+sf;
49
+ }
50
+
51
+ console.error({key, opts});
52
+ throw new TypeError('Invalid key');
53
+ }
54
+
55
+ module.exports = {getOwnKeys, isEmptyObject, keyName}
package/lib/obj/lock.js CHANGED
@@ -1,68 +1,2 @@
1
- // Import *most* required bits here.
2
- const {B, isObj, def} = require('../types');
3
-
4
- /**
5
- * Lock an object using Object.freeze()
6
- *
7
- * @param {object} obj - The object we want to lock.
8
- * @param {boolean} [clonable=true] Pass to `addClone()` first?
9
- * @param {object} [cloneOpts=null] Options for `addClone()`.
10
- * @param {boolean} [useSeal=false] Use Object.seal() instead of freeze.
11
- *
12
- * If cloneOpts is `null` then we will use the following:
13
- *
14
- * ```{mode: CLONE.DEF, addClone: true, addLock: true}```
15
- *
16
- * @return {object} The locked object.
17
- * @alias module:@lumjs/core/obj.lock
18
- */
19
- function lock(obj, clonable=true, cloneOpts=null, useSeal=false)
20
- {
21
- if (clonable)
22
- { // Add the clone method before freezing.
23
- if (!isObj(cloneOpts))
24
- {
25
- cloneOpts = {addClone: true, addLock: true};
26
- }
27
- addClone(obj, cloneOpts);
28
- }
29
-
30
- // Now freeze (or seal) the object.
31
- return (useSeal ? Object.seal(obj) : Object.freeze(obj));
32
- }
33
-
34
- exports.lock = lock;
35
-
36
- /**
37
- * Add a lock() method to an object.
38
- *
39
- * Adds a wrapper version of `lock()` to the object as a method.
40
- *
41
- * @param {object} obj - The object we're adding `lock()` to.
42
- * @param {object} [opts] - Options and defaults.
43
- * In addition to options specific to this function, any options
44
- * supported by `addClone()` may be specified here as well.
45
- * @param {object} [opts.lockDesc] Descriptor rules the `lock()` method.
46
- * @param {boolean} [opts.addClone=true] Default for `clonable` parameter.
47
- * @param {object} [opts.useSeal=false] Default for `useSeal` parameter.
48
- * @returns {object} `obj`
49
- * @alias module:@lumjs/core/obj.addLock
50
- */
51
- function addLock(obj, opts)
52
- {
53
- const defDesc = opts.lockDesc ?? {};
54
- defDesc.value = function(obj, cloneable, cloneOpts, useSeal)
55
- {
56
- if (typeof cloneable !== B) clonable = opts.addClone ?? true;
57
- if (!isObj(cloneOpts)) cloneOpts = opts; // Yup, just a raw copy.
58
- if (typeof useSeal !== B) useSeal = opts.useSeal ?? false;
59
- return lock(obj, cloneable, cloneOpts, useSeal);
60
- }
61
- return def(obj, 'lock', defDesc);
62
- }
63
-
64
- exports.addLock = addLock;
65
-
66
- // Importing addLock at the end, just because.
67
- const {addClone} = require('./clone');
68
-
1
+ // Moved to `cp` package
2
+ module.exports = require('@lumjs/cp/util');
@@ -1,6 +1,6 @@
1
1
  "use strict";
2
2
 
3
- const {F,S,isObj,needObj} = require('../types');
3
+ const {isObj} = require('../types/basics');
4
4
 
5
5
  const CLONE = 'clone';
6
6
  const clone = obj => Object.assign({}, obj);
@@ -48,19 +48,17 @@ const clone = obj => Object.assign({}, obj);
48
48
  * @alias module:@lumjs/core/obj.unlocked
49
49
  */
50
50
  function unlocked(obj, opts={})
51
- {
52
- needObj(obj, true, "invalid obj value");
53
-
51
+ {
54
52
  if (Object.isExtensible(obj))
55
53
  { // We're done here.
56
54
  return obj;
57
55
  }
58
56
 
59
- if (typeof opts === F)
57
+ if (typeof opts === 'function')
60
58
  {
61
59
  opts = {fn: opts}
62
60
  }
63
- else if (typeof opts === S)
61
+ else if (typeof opts === 'string')
64
62
  {
65
63
  opts = {method: opts}
66
64
  }
@@ -70,11 +68,11 @@ function unlocked(obj, opts={})
70
68
  throw new TypeError("invalid opts value");
71
69
  }
72
70
 
73
- const fn = (typeof opts.fn === F) ? opts.fn : clone;
74
- const meth = (typeof opts.method === S) ? opts.method.trim() : CLONE;
75
- const args = (Array.isArray(opts.args)) ? opts.args : [opts];
71
+ const fn = (typeof opts.fn === 'function') ? opts.fn : clone;
72
+ const meth = (typeof opts.method === 'string') ? opts.method.trim() : CLONE;
73
+ const args = (Array.isArray(opts.args)) ? opts.args : [opts];
76
74
 
77
- if (meth && typeof obj[meth] === F)
75
+ if (meth && typeof obj[meth] === 'function')
78
76
  { // Use a clone method
79
77
  return obj[meth](...args);
80
78
  }