@lumjs/compat 1.3.1 → 2.0.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/CHANGELOG.md CHANGED
@@ -6,6 +6,23 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
6
6
 
7
7
  ## [Unreleased]
8
8
 
9
+ ## [2.0.0]
10
+ ### Removed
11
+ - Literally everything from the 1.x package. It's left in the `v1` branch.
12
+ ### Added
13
+ - `exportModule()` function meant as a compatibility bridge between ES Modules
14
+ and CommonJS. It's simplistic and is only meant as a temporary measure to
15
+ add some layer of backwards-compatibility when migrating from CJS to ESM.
16
+ ### Changed
17
+ - Cleaned up README to trim anything to do with the removed sub-modules.
18
+ ### Fixed
19
+ - Added missing link references to this CHANGELOG.
20
+
21
+ ## [1.4.0] - 2023-01-06
22
+ ### Changed
23
+ - Made the `v4` set not use any `ModuleBuilder` features anymore.
24
+ - Moved `ModuleBuilder` from core into the `modulebuilder` namespace.
25
+
9
26
  ## [1.3.1] - 2022-10-18
10
27
  ### Fixed
11
28
  - The `v4/loadtracker` was trying to call a function from the old `Lum` global.
@@ -43,7 +60,9 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
43
60
  ### Added
44
61
  - Initial release.
45
62
 
46
- [Unreleased]: https://github.com/supernovus/lum.compat.js/compare/v1.3.1...HEAD
63
+ [Unreleased]: https://github.com/supernovus/lum.compat.js/compare/v2.0.0...HEAD
64
+ [2.0.0]: https://github.com/supernovus/lum.compat.js/compare/v1.4.0...v2.0.0
65
+ [1.4.0]: https://github.com/supernovus/lum.compat.js/compare/v1.3.1...v1.4.0
47
66
  [1.3.1]: https://github.com/supernovus/lum.compat.js/compare/v1.3.0...v1.3.1
48
67
  [1.3.0]: https://github.com/supernovus/lum.compat.js/compare/v1.2.0...v1.3.0
49
68
  [1.2.0]: https://github.com/supernovus/lum.compat.js/compare/v1.1.0...v1.2.0
package/README.md CHANGED
@@ -1,41 +1,20 @@
1
1
  # lum.compat.js
2
2
 
3
- As the libraries grow over time, features can change a lot, and backwards
4
- compatibility is not always easy to maintain.
3
+ Compatibility helpers.
5
4
 
6
- This library serves two purposes. The first is to be like the corresponding
7
- [lum/lum-compat](https://github.com/supernovus/lum.compat.php) PHP library,
8
- offering some generic compatibility functions.
5
+ As of version 2.0, this is a nano-package with zero dependencies that
6
+ currently provides a simple bridge between CommonJS and ES Modules.
9
7
 
10
- The second is to be a repository for some older, deprecated functionality from
11
- the [@lumjs/core](https://github.com/supernovus/lum.core.js) library.
8
+ ## Branches
12
9
 
13
- By keeping some of the old sub-modules here as optional plugins, I can keep
14
- legacy code up and running until I can rewrite it to no longer use those
15
- features.
10
+ ### `main`
16
11
 
17
- ## Entry Points
12
+ The `2.x` releases are from this branch.
18
13
 
19
- To keep features simple and separated, there's a few different entry points
20
- exported from this library.
14
+ ### `v1.x`
21
15
 
22
- ### `@lumjs/compat`
23
-
24
- The main entrypoint is reserved for generic compatibility functions.
25
-
26
- ### `@lumjs/compat/v4`
27
-
28
- The `v4` compatibility namespace. Includes all `v4` modules in one simple object.
29
-
30
- | Property | Description |
31
- | --------------- | --------------------------------------------------------- |
32
- | `descriptors` | The `v4/descriptors` module. |
33
- | `DESC` | The `v4/descriptors.DESC` factory object. |
34
- | `prop()` | The `v4/prop` module. |
35
- | `deprecated()` | The `v4/deprecated` module. |
36
- | `LoadTracker` | The `v4/loadtracker` module. |
37
- | `Promise` | The `v4/promise` module. |
38
- | `obj` | The `v4/object-helpers` module. |
16
+ The original package was a bunch of compatibility wrappers for [@lumjs/core],
17
+ and covered versions `1.0.0` through `1.4.0`.
39
18
 
40
19
  ## Official URLs
41
20
 
@@ -51,3 +30,5 @@ Timothy Totten <2010@totten.ca>
51
30
  ## License
52
31
 
53
32
  [MIT](https://spdx.org/licenses/MIT.html)
33
+
34
+ [@lumjs/core]: https://github.com/supernovus/lum.core.js
package/lib/index.js CHANGED
@@ -1,27 +1,138 @@
1
- // Compatibility jank.
2
-
3
- const semver = require('semver');
4
- const PKGJSON = 'package.json';
5
-
6
- // A super simplistic package info wrapper.
7
- class Package
8
- {
9
- constructor(pkg)
10
- {
11
- this.file = pkg+'/'+PKGJSON;
12
- this.info = require(this.file);
13
- this.version = semver.parse(this.info.version);
14
- }
1
+ /**
2
+ * Compatibility jank.
3
+ * @module @lumjs/compat
4
+ */
15
5
 
16
- satisfies(range, options)
17
- {
18
- return semver.satisfies(this.version, range, options);
19
- }
6
+ const ESM = '__esModule';
7
+ const DEF = 'default';
8
+
9
+ /**
10
+ * Make an exporter closure: `(key,value) => ...`;
11
+ *
12
+ * Properties added with the closure are configurable and enumerable,
13
+ * but not writable. It does NOT support accessors (getter/setter).
14
+ *
15
+ * This is mostly just designed for use in the exportModule() function.
16
+ * @param {(object|function)} obj - Target to export properties to.
17
+ * @returns {function}
18
+ */
19
+ function exporter(obj) {
20
+ return (key, value) => Object.defineProperty(obj, key, {
21
+ configurable: true,
22
+ enumerable: true,
23
+ value,
24
+ });
25
+ }
26
+
27
+ /**
28
+ * Property name filtering or renaming function.
29
+ * @callback module:@lumjs/compat~ExportUse
30
+ * @param {string} prop - Property name in ES Module.
31
+ * @returns {(string|false|undefined|null)} Property name for default export.
32
+ */
33
+
34
+ /**
35
+ * Export an ES Module type definition with some CommonJS compatibility.
36
+ *
37
+ * This takes an ES Module that was loaded using `require()`, and will
38
+ * attempt to create a CommonJS export that adds (most) named exports
39
+ * as properties of the default export.
40
+ *
41
+ * Example usage:
42
+ *
43
+ * ```js
44
+ * const { exportModule } = require('@lumjs/compat');
45
+ * module.exports = exportModule(require('./index.mjs'));
46
+ * ```
47
+ *
48
+ * @param {object} mod - The ES Module object.
49
+ *
50
+ * If this has a property named `default` that is either an `object`
51
+ * or a `function` then it will have properties added to it for each
52
+ * of the named exports (with a few exceptions configurable with options),
53
+ * and then the default export will be returned.
54
+ *
55
+ * NOTE: if the default export already has a property with an exported name,
56
+ * that export will be skipped rather than overwriting the existing value.
57
+ *
58
+ * If `mod.default` is NOT an object or function then the `mod` itself with
59
+ * no modifications will be the return value from this function.
60
+ *
61
+ * @param {object} [opts] Advanced options.
62
+ *
63
+ * @param {(string|symbol|false)} [opts.esmName="__esModule"] ESM Object key.
64
+ *
65
+ * If this is a string or symbol, it will be used as the name of a property
66
+ * to add to the default export that will contain the full `mod` object.
67
+ *
68
+ * Set this to `false` to disable this feature entirely.
69
+ *
70
+ * NOTE: unlike the named exports, this special property WILL overwrite an
71
+ * existing property with the same name, so be sure it's something that won't
72
+ * conflict. The default `__esModule` is meant to mask the boolean property
73
+ * of the same name that Node adds to ES Modules loaded via require().
74
+ *
75
+ * @param {boolean} [opts.skipDefault=true] Skip the `default` property?
76
+ *
77
+ * If true (default) it skips adding the `default` property, which would
78
+ * literally just be a link to itself and thus rather useless IMHO.
79
+ *
80
+ * You can set this to false if you really want the self-reference,
81
+ * or you could use `opts.map` to rename it to something else.
82
+ *
83
+ * @param {module:@lumjs/compat~ExportUse} [opts.use] Advanced use only!
84
+ *
85
+ * This will be called for every enumerable string-keyed property in `mod`.
86
+ * The context (`this`) in non-closures will be the `opts` object.
87
+ *
88
+ * It's return value will be used to determine the property name that
89
+ * will be used on the default export for the associated named export.
90
+ *
91
+ * - A `string` will be used as the property name on the default export.
92
+ * - `undefined` or `null` is the same as returning the name unchanged.
93
+ * - `false` may be used to skip adding that property entirely.
94
+ *
95
+ * For the majority of packages this feature is likely unnecessary,
96
+ * only use it if you have a really good reason to customize the exports!
97
+ *
98
+ * @returns {(object|function)} Either `mod.default` or `mod` itself.
99
+ * See above for details on how the return value is determined.
100
+ */
101
+ function exportModule(mod, opts={}) {
102
+ if (mod && typeof mod === 'object' && mod.default
103
+ && (typeof mod.default === 'object'
104
+ || typeof mod.default === 'function')
105
+ ) { // Has an object/function default export.
106
+ let modef = mod.default;
107
+ let esKey = opts.esmName ?? ESM;
108
+ let noDef = opts.skipDefault ?? true;
109
+ let use = (typeof opts.use === 'function') ? opts.use : null;
110
+ let addExport = exporter(modef);
111
+ let props = Object.keys(mod);
112
+
113
+ if (typeof esKey === 'string' || typeof esKey === 'symbol')
114
+ { // Add a reference to the full module.
115
+ addExport(esKey, mod);
116
+ }
117
+
118
+ for (let ep of props)
119
+ {
120
+ if (noDef && ep === DEF) continue;
121
+
122
+ let dp = use ? (use.call(opts, ep) ?? ep) : ep;
123
+ if (dp === false) continue;
124
+ if (Object.hasOwn(modef, dp)) continue;
125
+
126
+ let value = mod[ep];
127
+ if (value === undefined) continue;
128
+
129
+ addExport(dp, value);
130
+ }
20
131
 
21
- static new(pkg)
22
- {
23
- return new Package(pkg);
132
+ return modef;
24
133
  }
134
+
135
+ return mod;
25
136
  }
26
137
 
27
- exports.Package = Package;
138
+ module.exports = {exporter, exportModule}
package/package.json CHANGED
@@ -1,13 +1,13 @@
1
1
  {
2
2
  "name": "@lumjs/compat",
3
- "version": "1.3.1",
3
+ "version": "2.0.0",
4
4
  "main": "lib/index.js",
5
+ "engine":
6
+ {
7
+ "node": ">=22.0.0"
8
+ },
5
9
  "exports": {
6
10
  ".": "./lib/index.js",
7
- "./v4-meta": "./lib/v4/index.js",
8
- "./v4": "./lib/v4/index.js",
9
- "./v4/meta": "./lib/v4/index.js",
10
- "./v4/*": "./lib/v4/*.js",
11
11
  "./package.json": "./package.json"
12
12
  },
13
13
  "license": "MIT",
@@ -15,12 +15,7 @@
15
15
  "type": "git",
16
16
  "url": "https://github.com/supernovus/lum.compat.js.git"
17
17
  },
18
- "dependencies": {
19
- "@lumjs/core": "^1.7.1",
20
- "semver": "^7.3.7"
21
- },
22
- "scripts":
23
- {
18
+ "scripts": {
24
19
  "build-docs": "jsdoc -c ./jsdoc.json"
25
20
  }
26
21
  }
@@ -1,122 +0,0 @@
1
- /**
2
- * Mark a function/method/property as deprecated.
3
- *
4
- * Adds a warning to the Console that the method is deprecated.
5
- *
6
- * It can also optionally give example replacement code, and run a function
7
- * that will call the replacement code automatically.
8
- *
9
- * @param {string} name The name of the deprecated method/property/etc.
10
- *
11
- * This should actually be the full method signature, or at least the
12
- * signature matching what a call to the deprecated method made.
13
- *
14
- * So rather than 'someOldMethod', use 'MyClass.someOldMethod(foo, bar)'
15
- * as a more detailed name. This is only used in the Console warning.
16
- *
17
- * This is the only mandatory parameter.
18
- *
19
- * @param {mixed} [replace={}] Replacement options.
20
- *
21
- * If this is a {string}, it is the same as passing an {object} with
22
- * the following options specified:
23
- *
24
- * ```{msg: replace}```
25
- *
26
- * If it is a {function}, it is the same as passing an {object} with
27
- * the following options specified:
28
- *
29
- * ```{exec: replace, msg: true, strip: true}```
30
- *
31
- * If it is an {object}, then it will be a set of options:
32
- *
33
- * "exec" {function}
34
- *
35
- * If specified, this function will be called and the value returned.
36
- * No paramters are passed to the function, so it should be a simple
37
- * anonymous closure which simply calls the actual replacement code.
38
- *
39
- * "msg" {string|boolean}
40
- *
41
- * If this is a {string} it will be added to the warning output.
42
- *
43
- * If this is `true` and `exec` is set, we will extract the function
44
- * text using `exec.toString()` and add it to the warning output.
45
- *
46
- * In any other cases, no replacement message will be appended.
47
- *
48
- * "strip" {boolean}
49
- *
50
- * If this is `true`, then we will strip `'function() { return '`
51
- * from the start of the function text (whitespace ignored), as well
52
- * as stripping '}' from the very end of the function text.
53
- *
54
- * This is only applicable if `exec` is set, and `msg` is `true`.
55
- *
56
- * If the `replace` value is none of the above, it will be ignored.
57
- *
58
- * @return {mixed} The output is dependent on the parameters passed.
59
- *
60
- * If `replace` is a function or an object with
61
- *
62
- * In any other case the return value will be undefined.
63
- *
64
- * @alias module:@lumjs/compat/v4/deprecated
65
- */
66
- module.exports = function (name, replace={})
67
- {
68
- const DEP_MSG = ':Deprecated =>';
69
- const REP_MSG = ':replaceWith =>';
70
-
71
- if (typeof replace === S)
72
- { // A string replacement example only.
73
- replace = {msg: replace};
74
- }
75
- else if (typeof replace === F)
76
- { // A function replacement.
77
- replace = {exec: replace, msg: true, strip: true};
78
- }
79
- else if (!is_obj(replace))
80
- { // Not an object, that's not valid.
81
- replace = {};
82
- }
83
-
84
- const msgs = [DEP_MSG, name];
85
- const exec = (typeof replace.exec === F)
86
- ? replace.exec
87
- : null;
88
-
89
- if (exec && replace.msg === true)
90
- { // Extract the function text.
91
- const strip = (typeof replace.strip === B)
92
- ? replace.strip
93
- : false;
94
-
95
- let methtext = replace.exec.toString();
96
-
97
- if (strip)
98
- { // Remove wrapping anonymous closure function.
99
- methtext = methtext.replace(/^function\(\)\s*\{\s*(return\s*)?/, '');
100
- methtext = methtext.replace(/\s*\}$/, '');
101
- // Also support arrow function version.
102
- methtext = methtext.replace(/^\(\)\s*\=\>\s*/, '');
103
- }
104
-
105
- // Set the replacement msg to the method text.
106
- replace.msg = methtext;
107
- }
108
-
109
- if (typeof replace.msg === 'string')
110
- { // A replacement message.
111
- msgs.push(REP_MSG, replace.msg);
112
- }
113
-
114
- // Show the messages.
115
- console.warn(...msgs);
116
-
117
- // Finally, call the replacement function if it was defined.
118
- if (exec)
119
- {
120
- return exec();
121
- }
122
- }
@@ -1,217 +0,0 @@
1
- /**
2
- * Descriptors magic objects library.
3
- * @module @lumjs/compat/v4/descriptors
4
- */
5
-
6
- const core = require('@lumjs/core');
7
- const {InternalObjectId,Enum,types} = core;
8
- const {doesDescriptor,isObj,def,B,F,N} = types;
9
-
10
- // We moved getProperty() to the `@lumjs/core/obj` module.
11
- const getProperty = core.obj.getProperty;
12
- exports.getProperty = getProperty;
13
-
14
- const DESC_ID = new InternalObjectId({name: '$LumDescriptor'});
15
- const DESC_ADD = Enum(['ONE', 'SHORT', 'SET'], {flags: true});
16
-
17
- /**
18
- * Create a magic Descriptor object.
19
- * @param {object} desc - Descriptor template.
20
- * @param {object} [opts] - Options (to be documented.)
21
- * @returns {object} `desc`
22
- * @alias module:@lumjs/compat/v4/descriptors.descriptor
23
- */
24
- function descriptor(desc, opts={})
25
- {
26
- if (!isObj(opts)) throw new TypeError("Options must be an object");
27
-
28
- if (typeof desc === B)
29
- { // This is a special case.
30
- opts.accessorSafe = desc;
31
- opts.add = DESC_ADD.ONE;
32
- desc = {};
33
- }
34
-
35
- if (!isObj(desc))
36
- throw new TypeError("First parameter (desc) must be a descriptor template object");
37
-
38
- if (!Object.isExtensible(desc))
39
- throw new RangeError("First parameter (desc) must not be locked, sealed, frozen, etc.");
40
-
41
- const accessorSafe = (typeof opts.accessorSafe === B)
42
- ? opts.accessorSafe
43
- : (desc.writable === undefined);
44
-
45
- DESC_ID.tag(desc);
46
-
47
- // Add a function or other property.
48
- const add = def(desc);
49
-
50
- // Add a getter.
51
- function accessor(name, getter, setter)
52
- {
53
- const adef = {configurable: true};
54
- if (typeof getter === F) adef.get = getter;
55
- if (typeof setter === F) adef.set = setter;
56
- def(desc, name, adef);
57
- }
58
-
59
- add('accessorSafe', accessorSafe);
60
-
61
- add('whenDone', function(func)
62
- {
63
- if (typeof func === F)
64
- {
65
- add('done', func);
66
- }
67
- return this;
68
- });
69
-
70
- if (typeof opts.done === F)
71
- {
72
- desc.whenDone(opts.done);
73
- }
74
-
75
- add('setValue', function (val, noClean)
76
- {
77
- if (this.get !== undefined || this.set !== undefined)
78
- {
79
- console.error("Accessor properties already defined", this);
80
- }
81
- else
82
- {
83
- this.value = val;
84
- }
85
-
86
- return this;
87
- });
88
-
89
- if (accessorSafe)
90
- {
91
- function validate ()
92
- {
93
- if (this.value !== undefined)
94
- {
95
- console.error("Data 'value' property defined", this);
96
- return false;
97
- }
98
-
99
- for (const arg of arguments)
100
- { // All accessor arguments must be functions.
101
- if (typeof arg !== F) throw new TypeError("Parameter must be a function");
102
- }
103
-
104
- return true;
105
- }
106
-
107
- add('setGetter', function(func)
108
- {
109
- if (validate.call(this, func))
110
- this.get = func;
111
- return this;
112
- });
113
-
114
- add('setSetter', function(func)
115
- {
116
- if (validate.call(this, func))
117
- this.set = func;
118
- return this;
119
- });
120
-
121
- add('setAccessor', function(getter, setter)
122
- {
123
- if (validate.call(this, getter, setter))
124
- {
125
- this.get = getter;
126
- this.set = setter;
127
- }
128
- return this;
129
- });
130
-
131
- } // accessorSafe
132
-
133
- if (opts.add)
134
- {
135
- const addTypes
136
- = (typeof opts.add === N)
137
- ? opts.add
138
- : DESC_ADD.SET;
139
-
140
- function addBool(propname)
141
- {
142
- const setBool = function()
143
- {
144
- this[propname] = true;
145
- return this;
146
- }
147
-
148
- if (addTypes & DESC_ADD.ONE)
149
- {
150
- const aname = propname[0];
151
- accessor(aname, setBool);
152
- }
153
- if (addTypes & DESC_ADD.SHORT)
154
- {
155
- const aname = propname.substring(0,4);
156
- accessor(aname, setBool);
157
- }
158
- if (addTypes & DESC_ADD.SET)
159
- {
160
- const aname = 'set'+ucfirst(propname);
161
- accessor(aname, setBool);
162
- }
163
- }
164
-
165
- addBool('configurable');
166
- addBool('enumerable');
167
- addBool('writable');
168
-
169
- } // addBools
170
-
171
- // Is the descriptor ready to be used?
172
- accessor('isReady', function()
173
- {
174
- return doesDescriptor(this);
175
- });
176
-
177
- return desc;
178
- } // descriptor()
179
-
180
- exports.descriptor = descriptor;
181
-
182
- /**
183
- * Get a Descriptor object.
184
- * @param {object} desc - Either a Descriptor, or a descriptor template.
185
- * @returns {object}
186
- * @alias module:@lumjs/compat/v4/descriptors.getDescriptor
187
- */
188
- function getDescriptor(desc)
189
- {
190
- return DESC_ID.is(desc) ? desc : descriptor(desc);
191
- }
192
-
193
- exports.getDescriptor = getDescriptor;
194
-
195
- /**
196
- * A factory for building magic Descriptor objects.
197
- * @alias module:@lumjs/compat/v4/descriptors.DESC
198
- */
199
- const DESC =
200
- {
201
- get RO() { return descriptor(true) },
202
- get CONF() { return descriptor(true).c },
203
- get ENUM() { return descriptor(true).e },
204
- get WRITE() { return descriptor(false).w },
205
- get RW() { return descriptor(false).c.w },
206
- get DEF() { return descriptor(true).c.e },
207
- get OPEN() { return descriptor(false).c.e.w },
208
- }
209
-
210
- def(DESC)
211
- ('make', descriptor)
212
- ('is', DESC_ID.isFunction())
213
- ('get', getDescriptor)
214
- ('does', doesDescriptor)
215
- ('ADD', DESC_ADD);
216
-
217
- exports.DESC = DESC;
package/lib/v4/index.js DELETED
@@ -1,51 +0,0 @@
1
- /**
2
- * Lum.js v4 compatibility set.
3
- *
4
- * Now uses lazy-loading for all properties.
5
- *
6
- * @module @lumjs/compat/v4
7
- */
8
-
9
- const {can,from} = require('@lumjs/core').buildModule(module);
10
-
11
- /**
12
- * @name module:@lumjs/compat/v4.descriptors
13
- * @see module:@lumjs/compat/v4/descriptors
14
- */
15
- can('descriptors');
16
-
17
- /**
18
- * @name module:@lumjs/compat/v4.DESC
19
- * @see module:@lumjs/compat/v4/descriptors.DESC
20
- */
21
- from('descriptors', true, 'DESC');
22
-
23
- /**
24
- * @name module:@lumjs/compat/v4.prop
25
- * @see module:@lumjs/compat/v4/prop
26
- */
27
- can('prop');
28
-
29
- /**
30
- * @name module:@lumjs/compat/v4.deprecated
31
- * @see module:@lumjs/compat/v4/deprecated
32
- */
33
- can('deprecated');
34
-
35
- /**
36
- * @name module:@lumjs/compat/v4.LoadTracker
37
- * @see module:@lumjs/compat/v4/loadtracker
38
- */
39
- can('LoadTracker', true);
40
-
41
- /**
42
- * @name module:@lumjs/compat/v4.Promise
43
- * @see module:@lumjs/compat/v4/promise
44
- */
45
- can('Promise', true);
46
-
47
- /**
48
- * @name module:@lumjs/compat/v4.obj
49
- * @see module:@lumjs/compat/v4/object-helpers
50
- */
51
- can('obj', './object-helpers');