construct-new 1.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/README.md ADDED
@@ -0,0 +1,45 @@
1
+ # construct-new
2
+ Like the new operator, but as a function for convenience and familiarity.
3
+
4
+ ## Usage
5
+ What you would normally do:
6
+ ```js
7
+ class Foo {
8
+ constructor(name) {
9
+ this.name = name
10
+ }
11
+ print() {
12
+ console.log("Hi, I am " + this.name)
13
+ }
14
+ }
15
+ const myFoo = new Foo("bar")
16
+ myFoo.print() // output: Hi, I am bar
17
+ ```
18
+ What you would do with this:
19
+ ```js
20
+ const construct = require('construct-new')
21
+ class Foo {
22
+ constructor(name) {
23
+ this.name = name
24
+ }
25
+ print() {
26
+ console.log("Hi, I am " + this.name)
27
+ }
28
+ }
29
+ const myFoo = construct({
30
+ target: Foo,
31
+ args: ["bar"]
32
+ })
33
+
34
+ myFoo.print() // Hi, I am bar
35
+ ```
36
+ or
37
+ ```js
38
+ construct({
39
+ target: Foo,
40
+ args: ["bar"],
41
+ callback: (myFoo) => {
42
+ myFoo.print() // Hi, I am bar
43
+ }
44
+ })
45
+ ```
package/index.d.ts ADDED
@@ -0,0 +1,8 @@
1
+ type ConstructOptions = {
2
+ target?: new () => any,
3
+ args?: any[],
4
+ newTarget?: new () => any,
5
+ callback: () => void
6
+ }
7
+
8
+ export default function construct(options: ConstructOptions): any
package/index.js ADDED
@@ -0,0 +1,23 @@
1
+ var noop = require('noop6')
2
+ var emptyArray = require('empty/array')
3
+ var { immediateError, ErrorType } = require('immediate-error')
4
+ var { isNullOrUndefined } = require('node:util')
5
+
6
+ function construct({
7
+ target,
8
+ args,
9
+ newTarget,
10
+ callback
11
+ }) {
12
+ if (isNullOrUndefined(target)) {
13
+ immediateError("Target cannot be null when constructing a new instance of an object.", ErrorType.TypeError)
14
+ }
15
+ if (isNullOrUndefined(args)) args = emptyArray
16
+ if (isNullOrUndefined(newTarget)) newTarget = target
17
+ if (isNullOrUndefined(callback)) callback = noop
18
+ var result = Reflect.construct(target, args, newTarget)
19
+ callback(result)
20
+ return result
21
+ }
22
+
23
+ module.exports = construct
package/index.test.js ADDED
@@ -0,0 +1,16 @@
1
+ const construct = require('./index')
2
+ class Foo {
3
+ constructor(name) {
4
+ this.name = name
5
+ }
6
+ print() {
7
+ console.log("Hi, I am " + this.name)
8
+ }
9
+ }
10
+ construct({
11
+ target: Foo,
12
+ args: ["bar"],
13
+ callback: (myFoo) => {
14
+ myFoo.print() // Hi, I am bar
15
+ }
16
+ })
package/package.json ADDED
@@ -0,0 +1,29 @@
1
+ {
2
+ "name": "construct-new",
3
+ "version": "1.0.0",
4
+ "description": "Like the new operator, but a function.",
5
+ "main": "index.js",
6
+ "scripts": {
7
+ "test": "npm test"
8
+ },
9
+ "keywords": [
10
+ "instance",
11
+ "of",
12
+ "intrinsic",
13
+ "get",
14
+ "cache",
15
+ "ecmascript",
16
+ "javascript",
17
+ "is"
18
+ ],
19
+ "author": "tj-commits",
20
+ "license": "MIT",
21
+ "dependencies": {
22
+ "empty": "^0.10.1",
23
+ "immediate-error": "^4.0.0",
24
+ "noop6": "^1.0.9"
25
+ },
26
+ "devDependencies": {
27
+ "@types/node": "^22.5.5"
28
+ }
29
+ }