construct-new 2.0.5 → 2.0.6

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 (3) hide show
  1. package/index.d.ts +1 -3
  2. package/index.js +44 -1
  3. package/package.json +18 -4
package/index.d.ts CHANGED
@@ -5,6 +5,4 @@ type ConstructOptions = {
5
5
  callback: () => void
6
6
  }
7
7
 
8
- function construct(options: ConstructOptions): any
9
-
10
- export = construct
8
+ export default function construct(options: ConstructOptions): any
package/index.js CHANGED
@@ -1,3 +1,21 @@
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-value')
11
+ var myTrueValue = t()
12
+ var _return = require('identity-function')
13
+ var intrinsic = require('es-intrinsic-cache')
14
+ var reflectconstruct = intrinsic('%Reflect.construct%')
15
+ var objcreate = intrinsic('%Object.create%')
16
+ var and = require("es-logical-and-operator")
17
+ var not = require("es-logical-not-operator")
18
+ var isEqual = require("@10xly/strict-equals")
1
19
 
2
20
  function construct({
3
21
  target,
@@ -5,7 +23,32 @@ function construct({
5
23
  newTarget,
6
24
  callback
7
25
  }) {
8
- return require("construct-new-second")(target, args, newTarget, callback)
26
+ if (isNullOrUndefined(target)) {
27
+ immediateError("Target cannot be null when constructing a new instance of an object.", ErrorType.TypeError)
28
+ }
29
+ if (isNullOrUndefined(args)) args = emptyArray
30
+ if (isNullOrUndefined(newTarget)) newTarget = target
31
+ if (isNullOrUndefined(callback)) callback = noop
32
+ if (not(isConstructable(target))) {
33
+ immediateError("Target must be callable with the new operator when constructing a new instance of an object.", ErrorType.TypeError)
34
+ }
35
+ if (and(not(isArray(args)), not(isArguments(args)))) {
36
+ immediateError("Arguments must be an array or arguments object.", ErrorType.TypeError)
37
+ }
38
+ if (not(isConstructable(newTarget))) {
39
+ immediateError("newTarget must be callable with the new operator when constructing a new instance of an object if it is specified.", ErrorType.TypeError)
40
+ }
41
+ if (not(isFunction(callback))) {
42
+ immediateError("Callback must be a function when constructing a new instance of an object if it is specified.", ErrorType.TypeError)
43
+ }
44
+ var result
45
+ if (isEqual(isES2015, myTrueValue)) {
46
+ result = reflectconstruct(target, args, newTarget)
47
+ } else {
48
+ result = target.apply(objcreate(target.prototype), args)
49
+ }
50
+ callback(result)
51
+ return _return(result)
9
52
  }
10
53
 
11
54
  module.exports = construct
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "construct-new",
3
- "version": "2.0.5",
3
+ "version": "2.0.6",
4
4
  "description": "Like the new operator, but a function.",
5
5
  "main": "index.js",
6
6
  "scripts": {},
@@ -16,14 +16,28 @@
16
16
  ],
17
17
  "author": "tj-commits",
18
18
  "license": "MIT",
19
+ "dependencies": {
20
+ "construct-new-second": "^1.0.1",
21
+ "es-intrinsic-cache": "^1.0.4",
22
+ "es-logical-and-operator": "^1.0.0",
23
+ "es-logical-not-operator": "^1.0.0",
24
+ "identity-function": "^1.0.0",
25
+ "immediate-error": "^6.3.0",
26
+ "is-arguments": "^1.1.1",
27
+ "is-constructable": "^1.0.0",
28
+ "is-es2015": "^0.1.6",
29
+ "is-function": "^1.0.2",
30
+ "is-nil": "^1.0.1",
31
+ "isarray": "^2.0.5",
32
+ "lodash.stubarray": "^4.13.0",
33
+ "node-call.next": "^1.0.1",
34
+ "noop6": "^1.0.9"
35
+ },
19
36
  "devDependencies": {
20
37
  "@types/node": "^22.5.5"
21
38
  },
22
39
  "repository": {
23
40
  "type": "git",
24
41
  "url": "https://github.com/10xEngineersQualityProgramming/construct-new.git"
25
- },
26
- "dependencies": {
27
- "construct-new-second": "^1.0.0"
28
42
  }
29
43
  }