construct-new 1.0.0 → 1.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.
package/README.md CHANGED
@@ -43,3 +43,5 @@ construct({
43
43
  }
44
44
  })
45
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.js CHANGED
@@ -2,6 +2,14 @@ var noop = require('noop6')
2
2
  var emptyArray = require('empty/array')
3
3
  var { immediateError, ErrorType } = require('immediate-error')
4
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')
5
13
 
6
14
  function construct({
7
15
  target,
@@ -15,9 +23,26 @@ function construct({
15
23
  if (isNullOrUndefined(args)) args = emptyArray
16
24
  if (isNullOrUndefined(newTarget)) newTarget = target
17
25
  if (isNullOrUndefined(callback)) callback = noop
18
- var result = Reflect.construct(target, args, newTarget)
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
+ }
19
44
  callback(result)
20
- return result
45
+ return _return(result)
21
46
  }
22
47
 
23
48
  module.exports = construct
package/package.json CHANGED
@@ -1,11 +1,9 @@
1
1
  {
2
2
  "name": "construct-new",
3
- "version": "1.0.0",
3
+ "version": "1.0.1",
4
4
  "description": "Like the new operator, but a function.",
5
5
  "main": "index.js",
6
- "scripts": {
7
- "test": "npm test"
8
- },
6
+ "scripts": {},
9
7
  "keywords": [
10
8
  "instance",
11
9
  "of",
@@ -19,9 +17,16 @@
19
17
  "author": "tj-commits",
20
18
  "license": "MIT",
21
19
  "dependencies": {
20
+ "@_immo/return": "^1.1.1",
22
21
  "empty": "^0.10.1",
23
22
  "immediate-error": "^4.0.0",
24
- "noop6": "^1.0.9"
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"
25
30
  },
26
31
  "devDependencies": {
27
32
  "@types/node": "^22.5.5"
@@ -1,16 +1,15 @@
1
1
  const construct = require('./index')
2
2
  class Foo {
3
- constructor(name) {
3
+ constructor(name = "fart") {
4
4
  this.name = name
5
5
  }
6
6
  print() {
7
7
  console.log("Hi, I am " + this.name)
8
8
  }
9
9
  }
10
- construct({
10
+ const myFoo = construct({
11
11
  target: Foo,
12
- args: ["bar"],
13
- callback: (myFoo) => {
14
- myFoo.print() // Hi, I am bar
15
- }
12
+ args: ["bar"]
16
13
  })
14
+
15
+ myFoo.print() // Hi, I am bar