construct-new 1.0.1 → 2.0.1

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.
Files changed (5) hide show
  1. package/README.md +47 -47
  2. package/index.d.ts +8 -8
  3. package/index.js +48 -48
  4. package/package.json +38 -34
  5. package/t.js +0 -15
package/README.md CHANGED
@@ -1,47 +1,47 @@
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
- ```
46
-
47
- If the class doesn't take any arguments, you don't have to pass in the args property, it will still work like the args is an empty array.
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
+ ```
46
+
47
+ If the class doesn't take any arguments, you don't have to pass in the args property, it will still work like the args is an empty array.
package/index.d.ts CHANGED
@@ -1,8 +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
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 CHANGED
@@ -1,48 +1,48 @@
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
- var isES2015 = require('is-es2015')
6
- var isConstructable = require('is-constructable').isConstructable
7
- var isArray = require('isarray')
8
- var isArguments = require('is-arguments')
9
- var isFunction = require('is-function')
10
- var t = require('true')
11
- var myTrueValue = t()
12
- var _return = require('@_immo/return')
13
-
14
- function construct({
15
- target,
16
- args,
17
- newTarget,
18
- callback
19
- }) {
20
- if (isNullOrUndefined(target)) {
21
- immediateError("Target cannot be null when constructing a new instance of an object.", ErrorType.TypeError)
22
- }
23
- if (isNullOrUndefined(args)) args = emptyArray
24
- if (isNullOrUndefined(newTarget)) newTarget = target
25
- if (isNullOrUndefined(callback)) callback = noop
26
- if (!isConstructable(target)) {
27
- immediateError("Target must be callable with the new operator when constructing a new instance of an object.", ErrorType.TypeError)
28
- }
29
- if (!isArray(args) && !isArguments(args)) {
30
- immediateError("Arguments must be an array or arguments object.", ErrorType.TypeError)
31
- }
32
- if (!isConstructable(newTarget)) {
33
- immediateError("newTarget must be callable with the new operator when constructing a new instance of an object if it is specified.", ErrorType.TypeError)
34
- }
35
- if (!isFunction(callback)) {
36
- immediateError("Callback must be a function when constructing a new instance of an object if it is specified.", ErrorType.TypeError)
37
- }
38
- var result
39
- if (isES2015 === myTrueValue) {
40
- result = Reflect.construct(target, args, newTarget)
41
- } else {
42
- result = target.apply(Object.create(target.prototype), args)
43
- }
44
- callback(result)
45
- return _return(result)
46
- }
47
-
48
- module.exports = construct
1
+ var noop = require('noop6')
2
+ var emptyArray = []
3
+ var { immediateError, ErrorType } = require('immediate-error')
4
+ var isNullOrUndefined = require('is-nil')
5
+ var isES2015 = require('is-es2015')
6
+ var isConstructable = require('is-constructable').isConstructable
7
+ var isArray = require('isarray')
8
+ var isArguments = require('is-arguments')
9
+ var isFunction = require('is-function')
10
+ var t = require('true')
11
+ var myTrueValue = t()
12
+ var _return = require('@_immo/return')
13
+
14
+ function construct({
15
+ target,
16
+ args,
17
+ newTarget,
18
+ callback
19
+ }) {
20
+ if (isNullOrUndefined(target)) {
21
+ immediateError("Target cannot be null when constructing a new instance of an object.", ErrorType.TypeError)
22
+ }
23
+ if (isNullOrUndefined(args)) args = emptyArray
24
+ if (isNullOrUndefined(newTarget)) newTarget = target
25
+ if (isNullOrUndefined(callback)) callback = noop
26
+ if (!isConstructable(target)) {
27
+ immediateError("Target must be callable with the new operator when constructing a new instance of an object.", ErrorType.TypeError)
28
+ }
29
+ if (!isArray(args) && !isArguments(args)) {
30
+ immediateError("Arguments must be an array or arguments object.", ErrorType.TypeError)
31
+ }
32
+ if (!isConstructable(newTarget)) {
33
+ immediateError("newTarget must be callable with the new operator when constructing a new instance of an object if it is specified.", ErrorType.TypeError)
34
+ }
35
+ if (!isFunction(callback)) {
36
+ immediateError("Callback must be a function when constructing a new instance of an object if it is specified.", ErrorType.TypeError)
37
+ }
38
+ var result
39
+ if (isES2015 === myTrueValue) {
40
+ result = Reflect.construct(target, args, newTarget)
41
+ } else {
42
+ result = target.apply(Object.create(target.prototype), args)
43
+ }
44
+ callback(result)
45
+ return _return(result)
46
+ }
47
+
48
+ module.exports = construct
package/package.json CHANGED
@@ -1,34 +1,38 @@
1
- {
2
- "name": "construct-new",
3
- "version": "1.0.1",
4
- "description": "Like the new operator, but a function.",
5
- "main": "index.js",
6
- "scripts": {},
7
- "keywords": [
8
- "instance",
9
- "of",
10
- "intrinsic",
11
- "get",
12
- "cache",
13
- "ecmascript",
14
- "javascript",
15
- "is"
16
- ],
17
- "author": "tj-commits",
18
- "license": "MIT",
19
- "dependencies": {
20
- "@_immo/return": "^1.1.1",
21
- "empty": "^0.10.1",
22
- "immediate-error": "^4.0.0",
23
- "is-arguments": "^1.1.1",
24
- "is-constructable": "^1.0.0",
25
- "is-es2015": "^0.1.6",
26
- "is-function": "^1.0.2",
27
- "isarray": "^2.0.5",
28
- "noop6": "^1.0.9",
29
- "true": "^0.0.4"
30
- },
31
- "devDependencies": {
32
- "@types/node": "^22.5.5"
33
- }
34
- }
1
+ {
2
+ "name": "construct-new",
3
+ "version": "2.0.1",
4
+ "description": "Like the new operator, but a function.",
5
+ "main": "index.js",
6
+ "scripts": {},
7
+ "keywords": [
8
+ "instance",
9
+ "of",
10
+ "intrinsic",
11
+ "get",
12
+ "cache",
13
+ "ecmascript",
14
+ "javascript",
15
+ "is"
16
+ ],
17
+ "author": "tj-commits",
18
+ "license": "MIT",
19
+ "dependencies": {
20
+ "@_immo/return": "^1.1.1",
21
+ "immediate-error": "^4.0.0",
22
+ "is-arguments": "^1.1.1",
23
+ "is-constructable": "^1.0.0",
24
+ "is-es2015": "^0.1.6",
25
+ "is-function": "^1.0.2",
26
+ "is-nil": "^1.0.1",
27
+ "isarray": "^2.0.5",
28
+ "noop6": "^1.0.9",
29
+ "true": "^0.0.4"
30
+ },
31
+ "devDependencies": {
32
+ "@types/node": "^22.5.5"
33
+ },
34
+ "repository": {
35
+ "type": "git",
36
+ "url": "https://github.com/10xEngineersQualityProgramming/construct-new.git"
37
+ }
38
+ }
package/t.js DELETED
@@ -1,15 +0,0 @@
1
- const construct = require('./index')
2
- class Foo {
3
- constructor(name = "fart") {
4
- this.name = name
5
- }
6
- print() {
7
- console.log("Hi, I am " + this.name)
8
- }
9
- }
10
- const myFoo = construct({
11
- target: Foo,
12
- args: ["bar"]
13
- })
14
-
15
- myFoo.print() // Hi, I am bar