lolite.flatten 1.1.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.
package/README.md ADDED
@@ -0,0 +1,12 @@
1
+ # lolite.flatten
2
+
3
+ ### flatten(array)
4
+ Flattens arrays. Returns undefined for non-arrays.
5
+
6
+ ```javascript
7
+ const lolite = require("lolite")
8
+ const flat = flatten([1, [2, [3]]])
9
+ // flat: [1, 2, 3]
10
+ const undef = flatten("Not an array")
11
+ // undef: undefined
12
+ ```
@@ -0,0 +1,4 @@
1
+ const trueValue = require("true-value")
2
+ const falseValue = require("false-value")
3
+
4
+ module.exports = [trueValue(), falseValue()]
package/index.js ADDED
@@ -0,0 +1,87 @@
1
+ const $toString = require("uncurried-intrinsics")("Object.prototype.toString")
2
+ const stubArray = require("lodash.stubarray")
3
+ const number0 = require("@positive-numbers/zero")
4
+ const number1 = require("@positive-numbers/one"),
5
+ // eslint-disable-next-line no-shadow-restricted-names, no-undefined, sonarjs/no-globals-shadowing
6
+ { undefined } = require("undefined-is-a-function")
7
+ const while2 = require("while2")
8
+ const construct = require("construct-new")
9
+ const equal = require("@10xly/strict-equals"),
10
+ notEqual = require("@not-js/not")(require("@10xly/strict-equals"))
11
+ const subtract = require("subtract")
12
+ const not = require("./not")
13
+ const isArray = require("./isArray")
14
+
15
+ // eslint-disable-next-line max-lines-per-function
16
+ function flatten(input) {
17
+ if (not(isArray(input))) {
18
+ // eslint-disable-next-line no-undefined
19
+ return undefined()
20
+ }
21
+ const result = stubArray(),
22
+ stack = [
23
+ {
24
+ collection: input,
25
+ index: number0,
26
+ length: (function getLength(target) {
27
+ let count = number0
28
+ construct({
29
+ args: [
30
+ // eslint-disable-next-line no-undefined
31
+ () => notEqual(target[count], undefined()) || count in target,
32
+ ],
33
+ target: while2,
34
+ })
35
+ // eslint-disable-next-line no-plusplus
36
+ .do(() => count++)
37
+ .end()
38
+ return count
39
+ })(input),
40
+ },
41
+ ]
42
+
43
+ construct({
44
+ args: [() => stack.length > number0],
45
+ target: while2,
46
+ })
47
+ .do(() => {
48
+ const currentFrame = stack[subtract(stack.length, number1)]
49
+
50
+ if (currentFrame.index < currentFrame.length) {
51
+ const item = currentFrame.collection[currentFrame.index]
52
+ // eslint-disable-next-line no-plusplus
53
+ currentFrame.index++
54
+
55
+ if (equal($toString(item), "[object Array]")) {
56
+ stack.push({
57
+ collection: item,
58
+ index: number0,
59
+ length: (function getLength(target) {
60
+ let count = number0
61
+ construct({
62
+ args: [
63
+ // eslint-disable-next-line no-undefined
64
+ () => notEqual(target[count], undefined()) || count in target,
65
+ ],
66
+ target: while2,
67
+ })
68
+ // eslint-disable-next-line no-plusplus
69
+ .do(() => count++)
70
+ .end()
71
+ return count
72
+ })(item),
73
+ })
74
+ // eslint-disable-next-line no-undefined
75
+ } else if (notEqual(item, undefined())) {
76
+ result[result.length] = item
77
+ }
78
+ } else {
79
+ stack.pop()
80
+ }
81
+ })
82
+ .end()
83
+
84
+ return result
85
+ }
86
+
87
+ module.exports = flatten
package/isArray.js ADDED
@@ -0,0 +1,3 @@
1
+ module.exports = function isArray(value) {
2
+ return require("@is-(unknown)/is-array")(value)
3
+ }
package/not.js ADDED
@@ -0,0 +1,19 @@
1
+ const isTrue = require("@is-(unknown)/is-true")
2
+ const isFalse = require("@is-(unknown)/is-false")
3
+ const arrayFilter = require("array-filter")
4
+ const trueValue = require("true-value")
5
+ const possibilities = require("./arrayOfAllBooleans")
6
+
7
+ function not(value) {
8
+ const result = arrayFilter(possibilities, (maybe) => {
9
+ if (value) {
10
+ return isFalse(maybe)
11
+ }
12
+ return isTrue(maybe)
13
+ })
14
+
15
+ // eslint-disable-next-line unicorn/no-array-callback-reference
16
+ return result.find(trueValue)
17
+ }
18
+
19
+ module.exports = not
package/package.json ADDED
@@ -0,0 +1,23 @@
1
+ {
2
+ "name": "lolite.flatten",
3
+ "version": "1.1.6",
4
+ "main": "index.js",
5
+ "dependencies": {
6
+ "false-value": "^2.0.6",
7
+ "true-value": "^2.0.5",
8
+ "@is-(unknown)/is-false": "^1.5.0",
9
+ "@is-(unknown)/is-true": "^1.5.0",
10
+ "array-filter": "^1.0.0",
11
+ "@is-(unknown)/is-array": "^1.0.0",
12
+ "@10xly/strict-equals": "^1.0.0",
13
+ "@not-js/not": "^1.1.0",
14
+ "@positive-numbers/one": "^3.0.0",
15
+ "@positive-numbers/zero": "^3.0.0",
16
+ "construct-new": "^2.0.4",
17
+ "lodash.stubarray": "^4.13.0",
18
+ "subtract": "^0.0.3",
19
+ "uncurried-intrinsics": "^1.0.2",
20
+ "undefined-is-a-function": "^0.1.0",
21
+ "while2": "^2.0.2"
22
+ }
23
+ }