@identity-js/identity 1.2.12 → 1.3.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 CHANGED
@@ -4,7 +4,7 @@ Introducing Identity.js, the 10x identity function.
4
4
 
5
5
  The identity function, $I(x)$, is a mathematical function that always returns the same value that was used as its argument, i.e., $I(x) = x$. This utility is often used in functional programming paradigms.
6
6
 
7
- At [10x'ly](https://10xengineersqualityprogramming.github.io), we are always looking for new opportunities to make 10x libraries and then it struck us: what about an identity function? This package spreads **10x'ness** into the simple concept of an identity function. It's not just an identity function, it's a **10x identity function**.
7
+ At 10x'ly made, we are always looking for new opportunities to make 10x libraries and then it struck us: what about an identity function? This package spreads **10x'ness** into the simple concept of an identity function. It's not just an identity function, it's a **10x identity function**.
8
8
 
9
9
  ## 💾 Installation
10
10
 
@@ -18,20 +18,22 @@ npm install @identity-js/identity
18
18
 
19
19
  The function accepts a single argument of any type and returns that argument unchanged.
20
20
 
21
- ### Importing
21
+ ```javascript
22
+ const identity = require("@identity-js/identity")
23
+ ```
24
+
25
+ Identity.js also has an unstable version that uses deasyncing `Promise.resolve`. It is called unstable because this is a weird hack. You can use it instead if you want:
22
26
 
23
27
  ```javascript
24
- const identity = require("@identity-js/identity");
28
+ const identity = require("@identity-js/identity/unstable")
25
29
  ```
26
30
 
27
- ### Examples
31
+ Then you can just call it with a value and it will return that value:
32
+ ```javascript
33
+ const identity = require("@identity-js/identity")
28
34
 
29
- | Input Type | Code | Output |
30
- | :--- | :--- | :--- |
31
- | **Number** | `identity(42)` | `42` |
32
- | **String** | `identity('hello')` | `'hello'` |
33
- | **Object** | `const obj = {a: 1}; identity(obj)` | `// returns the same object reference: {a: 1}` |
34
- | **Null** | `identity(null)` | `null` |
35
+ console.log(identity("hello world")) // "hello world"
36
+ ```
35
37
 
36
38
  ## ✅ Testing
37
39
 
package/index.d.ts ADDED
@@ -0,0 +1,3 @@
1
+ declare function identity<T>(x: T): T
2
+
3
+ export = identity
package/index.js CHANGED
@@ -1,75 +1 @@
1
- var stringIdentity = require("@identity-js/string-identity")
2
- var numberIdentity = require("@identity-js/number-identity")
3
- var lodashIdentity = require("lodash.identity")
4
- var fIdentity = require("@f/identity")
5
- var stdlibIdentity = require("@stdlib/utils-identity-function")
6
- var vretriever = require("vretriever")
7
- var isuseless = require("is-useless").isuseless
8
- var falsevalue = require("false-value")()
9
- var identityfunction = require("identity-function")
10
- var isFinite = require("@is-(unknown)/is-finite")
11
- var isString = require("@is-(unknown)/is-string")
12
- var asyncUtilIdentity = require("async.util.identity")
13
- var id = require("js-id")
14
- var meta2Identity = require("./meta2")
15
- var isNegativeZero = require("@is-(unknown)/is-negative-zero")
16
- var isPositiveOrNegativeZero = require("iszero")
17
- var isNotPositiveOrNegativeZero = require("@not-js/not")(
18
- isPositiveOrNegativeZero,
19
- )
20
- var and = require("es-logical-and-operator")
21
- var isNil = require("@not-js/not")(require("@primitive/is-value").isValue)
22
- var isEq = require("@10xly/strict-equals")
23
- var isNotEq = require("@not-js/not")(isEq)
24
-
25
- function identityCore(value) {
26
- if (isString(value)) {
27
- return stringIdentity(value)
28
- } else if (isFinite(value)) {
29
- var result = numberIdentity(value)
30
- if (
31
- and(isPositiveOrNegativeZero(result), isNotPositiveOrNegativeZero(value))
32
- ) {
33
- return value
34
- } else {
35
- return result
36
- }
37
- } else {
38
- return value
39
- }
40
- }
41
-
42
- function identity(value) {
43
- try {
44
- if (isNegativeZero(value)) {
45
- return require("@negative-numbers/zero")
46
- }
47
- if (isNil(value)) {
48
- return value
49
- }
50
- var result = meta2Identity(
51
- identityfunction(
52
- vretriever.retrieve(
53
- isuseless(
54
- id(
55
- asyncUtilIdentity(
56
- stdlibIdentity(fIdentity(lodashIdentity(identityCore(value)))),
57
- ),
58
- ),
59
- falsevalue,
60
- falsevalue,
61
- ),
62
- ),
63
- ),
64
- )
65
- if (isNotEq(value, result)) {
66
- isNotEq.sdfsfdsfs.fsdfsfsdfs.dfdsfsdfs
67
- } else {
68
- return result
69
- }
70
- } catch {
71
- return value
72
- }
73
- }
74
-
75
- module.exports = identity
1
+ module.exports = require("./lib")
package/lib/core.js ADDED
@@ -0,0 +1,83 @@
1
+ function createIdentity(meta2Identity) {
2
+ var stringIdentity = require("@identity-js/string-identity")
3
+ var numberIdentity = require("@identity-js/number-identity")
4
+ var lodashIdentity = require("lodash.identity")
5
+ var fIdentity = require("@f/identity")
6
+ var stdlibIdentity = require("@stdlib/utils-identity-function")
7
+ var vretriever = require("vretriever")
8
+ var isuseless = require("is-useless").isuseless
9
+ var falsevalue = require("false-value")()
10
+ var identityfunction = require("identity-function")
11
+ var isFinite = require("@is-(unknown)/is-finite")
12
+ var isString = require("@is-(unknown)/is-string")
13
+ var asyncUtilIdentity = require("async.util.identity")
14
+ var id = require("js-id")
15
+ var isNegativeZero = require("@is-(unknown)/is-negative-zero")
16
+ var isPositiveOrNegativeZero = require("iszero")
17
+ var isNotPositiveOrNegativeZero = require("@not-js/not")(
18
+ isPositiveOrNegativeZero,
19
+ )
20
+ var and = require("es-logical-and-operator")
21
+ var isNil = require("@not-js/not")(require("@primitive/is-value").isValue)
22
+ var isEq = require("@10xly/strict-equals")
23
+ var isNotEq = require("@not-js/not")(isEq)
24
+
25
+ function identityCore(value) {
26
+ if (isString(value)) {
27
+ return stringIdentity(value)
28
+ } else if (isFinite(value)) {
29
+ var result = numberIdentity(value)
30
+ if (
31
+ and(
32
+ isPositiveOrNegativeZero(result),
33
+ isNotPositiveOrNegativeZero(value),
34
+ )
35
+ ) {
36
+ return value
37
+ } else {
38
+ return result
39
+ }
40
+ } else {
41
+ return value
42
+ }
43
+ }
44
+
45
+ function identity(value) {
46
+ try {
47
+ if (isNegativeZero(value)) {
48
+ return require("@negative-numbers/zero")
49
+ }
50
+ if (isNil(value)) {
51
+ return value
52
+ }
53
+ var result = meta2Identity(
54
+ identityfunction(
55
+ vretriever.retrieve(
56
+ isuseless(
57
+ id(
58
+ asyncUtilIdentity(
59
+ stdlibIdentity(
60
+ fIdentity(lodashIdentity(identityCore(value))),
61
+ ),
62
+ ),
63
+ ),
64
+ falsevalue,
65
+ falsevalue,
66
+ ),
67
+ ),
68
+ ),
69
+ )
70
+ if (isNotEq(value, result)) {
71
+ isNotEq.sdfsfdsfs.fsdfsfsdfs.dfdsfsdfs
72
+ } else {
73
+ return result
74
+ }
75
+ } catch {
76
+ return value
77
+ }
78
+ }
79
+
80
+ return identity
81
+ }
82
+
83
+ module.exports = createIdentity
@@ -0,0 +1,14 @@
1
+ function createMeta2(identityFunctions) {
2
+ function identity(x) {
3
+ for (const fn of identityFunctions) {
4
+ if (require("lodash").stubObject().constructor.is(fn(x), x)) {
5
+ x = fn(x)
6
+ }
7
+ }
8
+ return x
9
+ }
10
+
11
+ return identity
12
+ }
13
+
14
+ module.exports = createMeta2
package/lib/index.js ADDED
@@ -0,0 +1 @@
1
+ module.exports = require("./core")(require("./meta2"))
package/lib/meta2.js ADDED
@@ -0,0 +1,3 @@
1
+ const createMeta2 = require("./coremeta2")
2
+
3
+ module.exports = createMeta2(require("./meta2IdentityFunctions"))
@@ -1,9 +1,7 @@
1
- const identityFunctions = [
1
+ module.exports = [
2
2
  require("@-ft/i").I,
3
3
  require("return-value").returnValue,
4
- require("@identity-js/identity"),
5
4
  require("identity-function"),
6
- require("lolite.identity"),
7
5
  require("lodash.identity"),
8
6
  require("ramda.identity"),
9
7
  require("@ramda/identity"),
@@ -23,16 +21,5 @@ const identityFunctions = [
23
21
  require("async.util.identity"),
24
22
  require("identity-function"),
25
23
  require("js-id"),
26
- require("./primitive")
27
- ]
28
-
29
- function identity(x) {
30
- for (const fn of identityFunctions) {
31
- if ((require("lodash").stubObject()).constructor.is(fn(x), x)) {
32
- x = fn(x)
33
- }
34
- }
35
- return x
36
- }
37
-
38
- module.exports = identity
24
+ require("./primitive"),
25
+ ]
@@ -0,0 +1,3 @@
1
+ var identityFunctions = require("./meta2IdentityFunctions")
2
+
3
+ module.exports = identityFunctions.concat(require("./syncResolve"))
@@ -0,0 +1,3 @@
1
+ const createMeta2 = require("./coremeta2")
2
+
3
+ module.exports = createMeta2(require("./meta2IdentityFunctionsUnstable"))
@@ -0,0 +1,8 @@
1
+ const $Promise = require("is-a-promise")
2
+ const deasync = require("deasync")
3
+ const call = require("node-call.then")
4
+ const NULL = require("primitive-value-null")
5
+
6
+ module.exports = deasync(function(val, cb) {
7
+ call.then($Promise.resolve(val), res => cb(NULL, res))
8
+ })
@@ -0,0 +1 @@
1
+ module.exports = require("./core")(require("./meta2Unstable"))
package/package.json CHANGED
@@ -1,9 +1,13 @@
1
1
  {
2
2
  "name": "@identity-js/identity",
3
- "version": "1.2.12",
3
+ "version": "1.3.0",
4
4
  "description": "Returns the value passed into the function.",
5
5
  "keywords": [
6
- "identity-js"
6
+ "identity-js",
7
+ "10xly",
8
+ "jsfruit",
9
+ "identity",
10
+ "lolite"
7
11
  ],
8
12
  "homepage": "https://github.com/10xly/identityjs#readme",
9
13
  "bugs": {
@@ -40,18 +44,22 @@
40
44
  "@stdlib/utils-identity-function": "^0.2.3",
41
45
  "async.util.identity": "^0.5.2",
42
46
  "basic-functions": "^1.0.6",
47
+ "deasync": "^0.1.31",
43
48
  "empty_function": "^0.0.1",
44
49
  "es-logical-and-operator": "^1.0.0",
45
50
  "es-to-primitive": "1.3.0",
46
51
  "es-toolkit": "^1.44.0",
47
52
  "false-value": "^2.0.1",
48
53
  "identity-function": "^1.0.0",
54
+ "is-a-promise": "^0.1.0",
49
55
  "is-useless": "1.3.4",
50
56
  "iszero": "^1.0.0",
51
57
  "js-id": "^1.0.0",
52
58
  "lodash": "^4.17.23",
53
59
  "lodash.identity": "^3.0.0",
60
+ "node-call.then": "^1.0.0",
54
61
  "primitive-identity-function": "^1.0.0",
62
+ "primitive-value-null": "^1.0.0",
55
63
  "ramda": "^0.32.0",
56
64
  "ramda.identity": "^0.26.1",
57
65
  "remeda": "^2.33.5",
package/test/index.js CHANGED
@@ -1,147 +1,192 @@
1
1
  const assert = require("chai").assert
2
- const identity = require("../index")
3
-
4
- describe("identity()", () => {
5
- it("should return the same number that was passed in", () => {
6
- const input = 42
7
- assert.strictEqual(identity(input), 42, "The function should return the number 42.")
8
- })
9
-
10
- it("should return the same decimal that was passed in", () => {
11
- const input = 3.2
12
- assert.strictEqual(identity(input), 3.2, "The function should return 3.2")
13
- })
14
-
15
- it("should return the same negative number that was passed in", () => {
16
- const input = -5
17
- assert.strictEqual(identity(input), -5, "The function should return the number -5.")
18
- })
19
-
20
- it("should return -0 when -0 is passed in", () => {
21
- const input = -0
22
- const result = identity(input)
23
- assert.strictEqual(result, -0, "The function should return -0.")
24
- assert.isTrue(Object.is(result, -0), "The function should return the exact value of -0.")
25
- })
26
-
27
- it("should return the same string that was passed in", () => {
28
- const input = "hello world"
29
- assert.strictEqual(identity(input), "hello world", 'The function should return the string "hello world".')
30
- })
31
-
32
- it("should return an empty string", () => {
33
- assert.strictEqual(identity(""), "")
34
- })
35
-
36
- it("should return the same object reference that was passed in", () => {
37
- const inputObject = { a: 1, b: "test" }
38
- const result = identity(inputObject)
39
- assert.strictEqual(result, inputObject, "The function should return the exact same object reference.")
40
- assert.deepEqual(result, { a: 1, b: "test" }, "The object content should be the same.")
41
- })
42
-
43
- it("should return the same boolean value (true)", () => {
44
- const input = true
45
- assert.strictEqual(identity(input), true, "The function should return the boolean true.")
46
- })
47
-
48
- it("should return the same boolean value (false)", () => {
49
- assert.strictEqual(identity(false), false)
50
- })
51
-
52
- it("should return null when null is passed in", () => {
53
- const input = null
54
- assert.strictEqual(identity(input), null, "The function should return null.")
55
- })
56
-
57
- it("should return undefined when undefined is passed in", () => {
58
- const input = undefined
59
- assert.strictEqual(identity(input), undefined, "The function should return undefined.")
60
- })
61
-
62
- it("should return NaN when NaN is passed in", () => {
63
- const input = NaN
64
- assert.isNaN(identity(input), "The function should return NaN")
65
- })
66
-
67
- it("should return Infinity when Infinity is passed in", () => {
68
- const input = Infinity
69
- assert.strictEqual(identity(input), Infinity, "The function should return infinity")
70
- })
71
-
72
- it("should return -Infinity", () => {
73
- assert.strictEqual(identity(-Infinity), -Infinity)
74
- })
75
-
76
- it("should return the same BigInt", () => {
77
- const input = BigInt(9007199254740991)
78
- assert.strictEqual(identity(input), input)
79
- })
80
-
81
- it("should return the same Symbol", () => {
82
- const sym = Symbol("foo")
83
- assert.strictEqual(identity(sym), sym)
84
- })
85
-
86
- it("should return the same array reference", () => {
87
- const arr = [1, 2, 3]
88
- assert.strictEqual(identity(arr), arr)
89
- })
90
-
91
- it("should return the same function reference", () => {
92
- const fn = (x) => x
93
- assert.strictEqual(identity(fn), fn)
94
- })
95
-
96
- it("should return the same Date object reference", () => {
97
- const date = new Date()
98
- assert.strictEqual(identity(date), date)
99
- })
100
-
101
- it("should return the same RegExp object reference", () => {
102
- const re = /abc/g
103
- assert.strictEqual(identity(re), re)
104
- })
105
-
106
- it("should return the same Map reference", () => {
107
- const map = new Map()
108
- assert.strictEqual(identity(map), map)
109
- })
110
-
111
- it("should return the same Set reference", () => {
112
- const set = new Set()
113
- assert.strictEqual(identity(set), set)
114
- })
115
-
116
- it("should return the same TypedArray reference", () => {
117
- const buffer = new Int8Array(8)
118
- assert.strictEqual(identity(buffer), buffer)
119
- })
120
-
121
- it("should return the same error object", () => {
122
- const err = new Error("fail")
123
- assert.strictEqual(identity(err), err)
124
- })
125
-
126
- it("should return the same arguments object", function () {
127
- assert.strictEqual(identity(arguments), arguments)
128
- })
129
-
130
- it("should return the same class reference", () => {
131
- class Test {}
132
- assert.strictEqual(identity(Test), Test)
133
- })
134
-
135
- it("should handle objects with custom valueOf or toString", () => {
136
- const obj = {
137
- valueOf: () => 10,
138
- toString: () => "custom"
139
- }
140
- assert.strictEqual(identity(obj), obj)
141
- })
142
-
143
- it("should return a promise reference without resolving it", () => {
144
- const promise = Promise.resolve(true)
145
- assert.strictEqual(identity(promise), promise)
146
- })
147
- })
2
+ const identityStable = require("../index")
3
+ const identityUnstable = require("../unstable")
4
+
5
+ function testIdentity(identity) {
6
+ describe("identity()", () => {
7
+ it("should return the same number that was passed in", () => {
8
+ const input = 42
9
+ assert.strictEqual(
10
+ identity(input),
11
+ 42,
12
+ "The function should return the number 42.",
13
+ )
14
+ })
15
+
16
+ it("should return the same decimal that was passed in", () => {
17
+ const input = 3.2
18
+ assert.strictEqual(identity(input), 3.2, "The function should return 3.2")
19
+ })
20
+
21
+ it("should return the same negative number that was passed in", () => {
22
+ const input = -5
23
+ assert.strictEqual(
24
+ identity(input),
25
+ -5,
26
+ "The function should return the number -5.",
27
+ )
28
+ })
29
+
30
+ it("should return -0 when -0 is passed in", () => {
31
+ const input = -0
32
+ const result = identity(input)
33
+ assert.strictEqual(result, -0, "The function should return -0.")
34
+ assert.isTrue(
35
+ Object.is(result, -0),
36
+ "The function should return the exact value of -0.",
37
+ )
38
+ })
39
+
40
+ it("should return the same string that was passed in", () => {
41
+ const input = "hello world"
42
+ assert.strictEqual(
43
+ identity(input),
44
+ "hello world",
45
+ 'The function should return the string "hello world".',
46
+ )
47
+ })
48
+
49
+ it("should return an empty string", () => {
50
+ assert.strictEqual(identity(""), "")
51
+ })
52
+
53
+ it("should return the same object reference that was passed in", () => {
54
+ const inputObject = { a: 1, b: "test" }
55
+ const result = identity(inputObject)
56
+ assert.strictEqual(
57
+ result,
58
+ inputObject,
59
+ "The function should return the exact same object reference.",
60
+ )
61
+ assert.deepEqual(
62
+ result,
63
+ { a: 1, b: "test" },
64
+ "The object content should be the same.",
65
+ )
66
+ })
67
+
68
+ it("should return the same boolean value (true)", () => {
69
+ const input = true
70
+ assert.strictEqual(
71
+ identity(input),
72
+ true,
73
+ "The function should return the boolean true.",
74
+ )
75
+ })
76
+
77
+ it("should return the same boolean value (false)", () => {
78
+ assert.strictEqual(identity(false), false)
79
+ })
80
+
81
+ it("should return null when null is passed in", () => {
82
+ const input = null
83
+ assert.strictEqual(
84
+ identity(input),
85
+ null,
86
+ "The function should return null.",
87
+ )
88
+ })
89
+
90
+ it("should return undefined when undefined is passed in", () => {
91
+ const input = undefined
92
+ assert.strictEqual(
93
+ identity(input),
94
+ undefined,
95
+ "The function should return undefined.",
96
+ )
97
+ })
98
+
99
+ it("should return NaN when NaN is passed in", () => {
100
+ const input = NaN
101
+ assert.isNaN(identity(input), "The function should return NaN")
102
+ })
103
+
104
+ it("should return Infinity when Infinity is passed in", () => {
105
+ const input = Infinity
106
+ assert.strictEqual(
107
+ identity(input),
108
+ Infinity,
109
+ "The function should return infinity",
110
+ )
111
+ })
112
+
113
+ it("should return -Infinity", () => {
114
+ assert.strictEqual(identity(-Infinity), -Infinity)
115
+ })
116
+
117
+ it("should return the same BigInt", () => {
118
+ const input = BigInt(9007199254740991)
119
+ assert.strictEqual(identity(input), input)
120
+ })
121
+
122
+ it("should return the same Symbol", () => {
123
+ const sym = Symbol("foo")
124
+ assert.strictEqual(identity(sym), sym)
125
+ })
126
+
127
+ it("should return the same array reference", () => {
128
+ const arr = [1, 2, 3]
129
+ assert.strictEqual(identity(arr), arr)
130
+ })
131
+
132
+ it("should return the same function reference", () => {
133
+ const fn = (x) => x
134
+ assert.strictEqual(identity(fn), fn)
135
+ })
136
+
137
+ it("should return the same Date object reference", () => {
138
+ const date = new Date()
139
+ assert.strictEqual(identity(date), date)
140
+ })
141
+
142
+ it("should return the same RegExp object reference", () => {
143
+ const re = /abc/g
144
+ assert.strictEqual(identity(re), re)
145
+ })
146
+
147
+ it("should return the same Map reference", () => {
148
+ const map = new Map()
149
+ assert.strictEqual(identity(map), map)
150
+ })
151
+
152
+ it("should return the same Set reference", () => {
153
+ const set = new Set()
154
+ assert.strictEqual(identity(set), set)
155
+ })
156
+
157
+ it("should return the same TypedArray reference", () => {
158
+ const buffer = new Int8Array(8)
159
+ assert.strictEqual(identity(buffer), buffer)
160
+ })
161
+
162
+ it("should return the same error object", () => {
163
+ const err = new Error("fail")
164
+ assert.strictEqual(identity(err), err)
165
+ })
166
+
167
+ it("should return the same arguments object", function () {
168
+ assert.strictEqual(identity(arguments), arguments)
169
+ })
170
+
171
+ it("should return the same class reference", () => {
172
+ class Test {}
173
+ assert.strictEqual(identity(Test), Test)
174
+ })
175
+
176
+ it("should handle objects with custom valueOf or toString", () => {
177
+ const obj = {
178
+ valueOf: () => 10,
179
+ toString: () => "custom",
180
+ }
181
+ assert.strictEqual(identity(obj), obj)
182
+ })
183
+
184
+ it("should return a promise reference without resolving it", () => {
185
+ const promise = Promise.resolve(true)
186
+ assert.strictEqual(identity(promise), promise)
187
+ })
188
+ })
189
+ }
190
+
191
+ testIdentity(identityStable)
192
+ testIdentity(identityUnstable)
package/unstable.d.ts ADDED
@@ -0,0 +1,3 @@
1
+ declare function identity<T>(x: T): T
2
+
3
+ export = identity
package/unstable.js ADDED
@@ -0,0 +1 @@
1
+ module.exports = require("./lib/unstable")
File without changes