uncurried-intrinsics 1.0.0 → 1.0.2

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/README.md +56 -26
  2. package/index.js +8 -3
  3. package/package.json +1 -1
package/README.md CHANGED
@@ -15,70 +15,100 @@ npm install uncurry-intrinsics
15
15
  ## Usage
16
16
 
17
17
  ```javascript
18
- var uncurryIntrinsic = require("uncurry-intrinsics");
18
+ var uncurryIntrinsic = require("uncurry-intrinsics")
19
19
 
20
20
  // Get uncurried Array.prototype.slice
21
- var slice = uncurryIntrinsic("Array.prototype.slice");
22
- slice([1, 2, 3, 4, 5], 1, 3); // [2, 3]
21
+ var slice = uncurryIntrinsic("Array.prototype.slice")
22
+ slice([1, 2, 3, 4, 5], 1, 3) // [2, 3]
23
23
 
24
24
  // Get uncurried String.prototype.split
25
- var split = uncurryIntrinsic("String.prototype.split");
26
- split("foo-bar-baz", "-"); // ["foo", "bar", "baz"]
25
+ var split = uncurryIntrinsic("String.prototype.split")
26
+ split("foo-bar-baz", "-") // ["foo", "bar", "baz"]
27
27
 
28
28
  // Get uncurried Array.prototype.map
29
- var map = uncurryIntrinsic("Array.prototype.map");
30
- var doubled = map([1, 2, 3], function(x) { return x * 2; }); // [2, 4, 6]
29
+ var map = uncurryIntrinsic("Array.prototype.map")
30
+ var doubled = map([1, 2, 3], function (x) {
31
+ return x * 2
32
+ }) // [2, 4, 6]
31
33
  ```
32
34
 
33
35
  ## API
34
36
 
35
- ### `uncurryIntrinsic(intrinsicName)`
37
+ ### `uncurryIntrinsic(intrinsicName[, allowMissing])`
36
38
 
37
39
  Gets an intrinsic by name and returns its uncurried version where `this` becomes the first parameter.
38
40
 
39
41
  **Parameters:**
42
+
40
43
  - `intrinsicName` (string): The name of the intrinsic to get and uncurry (e.g., `"Array.prototype.slice"`)
44
+ - `allowMissing` (boolean, optional): If `true`, returns `null` or `undefined` for missing intrinsics instead of throwing an error. Default: `false`
41
45
 
42
46
  **Returns:**
43
- - (Function): The uncurried intrinsic function
47
+
48
+ - (Function|null|undefined): The uncurried intrinsic function, or `null`/`undefined` if `allowMissing` is `true` and the intrinsic doesn't exist
49
+
50
+ **Throws:**
51
+
52
+ - Error if the intrinsic is not available and `allowMissing` is `false`
44
53
 
45
54
  ## Examples
46
55
 
47
56
  ### Array methods
48
57
 
49
58
  ```javascript
50
- var uncurryIntrinsic = require("uncurry-intrinsics");
59
+ var uncurryIntrinsic = require("uncurry-intrinsics")
51
60
 
52
- var push = uncurryIntrinsic("Array.prototype.push");
53
- var join = uncurryIntrinsic("Array.prototype.join");
61
+ var push = uncurryIntrinsic("Array.prototype.push")
62
+ var join = uncurryIntrinsic("Array.prototype.join")
54
63
 
55
- var arr = [1, 2, 3];
56
- push(arr, 4, 5); // 5
57
- join(arr, ", "); // "1, 2, 3, 4, 5"
64
+ var arr = [1, 2, 3]
65
+ push(arr, 4, 5) // 5
66
+ join(arr, ", ") // "1, 2, 3, 4, 5"
58
67
  ```
59
68
 
60
69
  ### String methods
61
70
 
62
71
  ```javascript
63
- var uncurryIntrinsic = require("uncurry-intrinsics");
72
+ var uncurryIntrinsic = require("uncurry-intrinsics")
64
73
 
65
- var charAt = uncurryIntrinsic("String.prototype.charAt");
66
- var toUpperCase = uncurryIntrinsic("String.prototype.toUpperCase");
74
+ var charAt = uncurryIntrinsic("String.prototype.charAt")
75
+ var toUpperCase = uncurryIntrinsic("String.prototype.toUpperCase")
67
76
 
68
- charAt("hello", 0); // "h"
69
- toUpperCase("hello"); // "HELLO"
77
+ charAt("hello", 0) // "h"
78
+ toUpperCase("hello") // "HELLO"
70
79
  ```
71
80
 
72
81
  ### Object methods
73
82
 
74
83
  ```javascript
75
- var uncurryIntrinsic = require("uncurry-intrinsics");
84
+ var uncurryIntrinsic = require("uncurry-intrinsics")
76
85
 
77
- var hasOwnProperty = uncurryIntrinsic("Object.prototype.hasOwnProperty");
86
+ var hasOwnProperty = uncurryIntrinsic("Object.prototype.hasOwnProperty")
78
87
 
79
- var obj = { foo: 1, bar: 2 };
80
- hasOwnProperty(obj, "foo"); // true
81
- hasOwnProperty(obj, "baz"); // false
88
+ var obj = { foo: 1, bar: 2 }
89
+ hasOwnProperty(obj, "foo") // true
90
+ hasOwnProperty(obj, "baz") // false
91
+ ```
92
+
93
+ ### Handling missing intrinsics
94
+
95
+ ```javascript
96
+ var uncurryIntrinsic = require("uncurry-intrinsics")
97
+
98
+ // Throws an error if the intrinsic doesn't exist
99
+ try {
100
+ var missing = uncurryIntrinsic("NonExistent.prototype.method")
101
+ } catch (e) {
102
+ console.log(e.message) // 'Intrinsic "NonExistent.prototype.method" is not available'
103
+ }
104
+
105
+ // Returns null/undefined if allowMissing is true
106
+ var maybeExists = uncurryIntrinsic("Reflect.apply", true)
107
+ if (maybeExists) {
108
+ // Use the intrinsic
109
+ } else {
110
+ // Handle missing intrinsic
111
+ }
82
112
  ```
83
113
 
84
114
  ## See Also
@@ -88,4 +118,4 @@ hasOwnProperty(obj, "baz"); // false
88
118
 
89
119
  ## License
90
120
 
91
- MIT
121
+ MIT
package/index.js CHANGED
@@ -6,13 +6,18 @@ var uncurryThis = require("uncurry-x")
6
6
  /**
7
7
  * Gets an intrinsic and returns its uncurried version
8
8
  * @param {string} intrinsicName - The name of the intrinsic (e.g., "Array.prototype.slice")
9
- * @param {boolean} allowMissing - Whether to allow missing intrinsics
10
- * @returns {Function} The uncurried intrinsic function
9
+ * @param {boolean} [allowMissing=false] - Whether to allow missing intrinsics (returns null/undefined instead of throwing)
10
+ * @returns {Function|null|undefined} The uncurried intrinsic function, or null/undefined if allowMissing is true and intrinsic doesn't exist
11
+ * @throws {Error} If the intrinsic is not available and allowMissing is false, or if GetIntrinsic fails
11
12
  */
12
13
  function uncurryIntrinsic(intrinsicName, allowMissing) {
13
14
  var intrinsic
14
15
  try {
15
- intrinsic = GetIntrinsic(intrinsicName, allowMissing)
16
+ if (allowMissing != null) {
17
+ intrinsic = GetIntrinsic(intrinsicName, allowMissing)
18
+ } else {
19
+ intrinsic = GetIntrinsic(intrinsicName)
20
+ }
16
21
  } catch (e) {
17
22
  throw new Error(
18
23
  'Failed to get intrinsic "' + intrinsicName + '": ' + e.message
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "uncurried-intrinsics",
3
- "version": "1.0.0",
3
+ "version": "1.0.2",
4
4
  "description": "Combines es-intrinsic-cache and uncurry-x to get and uncurry JavaScript intrinsics in one step",
5
5
  "keywords": [
6
6
  "intrinsics",