functionalscript 0.0.187 → 0.0.190
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/json/index.js +22 -18
- package/json/test.js +5 -0
- package/package.json +1 -1
- package/sequence/index.js +6 -1
- package/sequence/test.js +5 -3
package/json/index.js
CHANGED
|
@@ -1,7 +1,6 @@
|
|
|
1
1
|
const seq = require('../sequence')
|
|
2
2
|
const map = require('../map')
|
|
3
3
|
const op = require('../sequence/operator')
|
|
4
|
-
const { todo } = require('../dev')
|
|
5
4
|
|
|
6
5
|
/**
|
|
7
6
|
* @typedef {{
|
|
@@ -27,20 +26,27 @@ const addProperty = value => {
|
|
|
27
26
|
}
|
|
28
27
|
|
|
29
28
|
/** @type {(kv: readonly[string, seq.Sequence<string>]) => seq.Sequence<string>} */
|
|
30
|
-
const property = ([k, v]) =>
|
|
31
|
-
let r = seq.one(JSON.stringify(k))
|
|
32
|
-
r = seq.concat(r)(seq.one(':'))
|
|
33
|
-
return seq.concat(r)(v)
|
|
34
|
-
}
|
|
29
|
+
const property = ([k, v]) => seq.concat(seq.one(JSON.stringify(k)), seq.one(':'), v)
|
|
35
30
|
|
|
36
31
|
/** @type {op.Scan<seq.Sequence<string>, seq.Sequence<string>>} */
|
|
37
|
-
const commaValue = a => [seq.concat(seq.one(',')
|
|
32
|
+
const commaValue = a => [seq.concat(seq.one(','), a), commaValue]
|
|
38
33
|
|
|
39
34
|
/** @type {op.Scan<seq.Sequence<string>, seq.Sequence<string>>} */
|
|
40
35
|
const joinScan = value => [value, commaValue]
|
|
41
36
|
|
|
42
|
-
/** @type {seq.SequenceMap<seq.Sequence<string>,
|
|
43
|
-
const join = seq.scan(joinScan)
|
|
37
|
+
/** @type {seq.SequenceMap<seq.Sequence<string>, string>} */
|
|
38
|
+
const join = input => seq.flat(seq.scan(joinScan)(input))
|
|
39
|
+
|
|
40
|
+
/** @type {(open: string) => (close: string) => (input: seq.Sequence<seq.Sequence<string>>) => seq.Sequence<string>} */
|
|
41
|
+
const list = open => close => {
|
|
42
|
+
const seqOpen = seq.one(open)
|
|
43
|
+
const seqClose = seq.one(close)
|
|
44
|
+
return input => seq.concat(seqOpen, join(input), seqClose)
|
|
45
|
+
}
|
|
46
|
+
|
|
47
|
+
const objectList = list('{')('}')
|
|
48
|
+
|
|
49
|
+
const arrayList = list('[')(']')
|
|
44
50
|
|
|
45
51
|
/** @type {(object: Object) => seq.Sequence<string>} */
|
|
46
52
|
const objectStringify = object => {
|
|
@@ -49,17 +55,11 @@ const objectStringify = object => {
|
|
|
49
55
|
for (const [k, v] of Object.entries(object)) {
|
|
50
56
|
m = m.set(k)(stringSeq(v))
|
|
51
57
|
}
|
|
52
|
-
|
|
53
|
-
const result = seq.concat(seq.one('{'))(seq.flat(properties))
|
|
54
|
-
return seq.concat(result)(seq.one('}'))
|
|
58
|
+
return objectList(seq.map(property)(m.entries))
|
|
55
59
|
}
|
|
56
60
|
|
|
57
61
|
/** @type {(array: Array) => seq.Sequence<string>} */
|
|
58
|
-
const arrayStringify = array =>
|
|
59
|
-
let a = seq.flat(join(seq.map(stringSeq)(seq.fromArray(array))))
|
|
60
|
-
const s = seq.concat(seq.one('['))(a)
|
|
61
|
-
return seq.concat(s)(seq.one(']'))
|
|
62
|
-
}
|
|
62
|
+
const arrayStringify = array => arrayList(seq.map(stringSeq)(seq.fromArray(array)))
|
|
63
63
|
|
|
64
64
|
/** @type {(value: Json) => seq.Sequence<string>} */
|
|
65
65
|
const stringSeq = value => {
|
|
@@ -77,7 +77,11 @@ const stringSeq = value => {
|
|
|
77
77
|
}
|
|
78
78
|
}
|
|
79
79
|
|
|
80
|
-
/**
|
|
80
|
+
/**
|
|
81
|
+
* A deterministic version of `JSON.stringify`
|
|
82
|
+
*
|
|
83
|
+
* @type {(value: Json) => string}
|
|
84
|
+
*/
|
|
81
85
|
const stringify = value => seq.join('')(stringSeq(value))
|
|
82
86
|
|
|
83
87
|
module.exports = {
|
package/json/test.js
CHANGED
|
@@ -11,3 +11,8 @@ if (json.addProperty("Hello")([])({}) !== "Hello") { throw 'error' }
|
|
|
11
11
|
const x = json.stringify(json.addProperty("Hello")(['a'])({c:[],b:12}))
|
|
12
12
|
if (x !== '{"a":"Hello","b":12,"c":[]}') { throw x }
|
|
13
13
|
}
|
|
14
|
+
|
|
15
|
+
{
|
|
16
|
+
const x = json.stringify(json.addProperty("Hello")(['a', 'x'])({ a: { y: [24] }, c: [], b: 12 }))
|
|
17
|
+
if (x !== '{"a":{"x":"Hello","y":[24]},"b":12,"c":[]}') { throw x }
|
|
18
|
+
}
|
package/package.json
CHANGED
package/sequence/index.js
CHANGED
|
@@ -93,7 +93,10 @@ const fromArray = a => {
|
|
|
93
93
|
}
|
|
94
94
|
|
|
95
95
|
/** @type {<T>(a: Sequence<T>) => SequenceMap<T, T>} */
|
|
96
|
-
const
|
|
96
|
+
const concat2 = a => b => [a, b]
|
|
97
|
+
|
|
98
|
+
/** @type {<T>(...array: readonly Sequence<T>[]) => Sequence<T>} */
|
|
99
|
+
const concat = (...a) => flat(fromArray(a))
|
|
97
100
|
|
|
98
101
|
/** @type {<T, R>(f: (value: T) => Sequence<R>) => SequenceMap<T, R>} */
|
|
99
102
|
const flatMap = f => input => () => {
|
|
@@ -252,6 +255,8 @@ module.exports = {
|
|
|
252
255
|
/** @readonly */
|
|
253
256
|
concat,
|
|
254
257
|
/** @readonly */
|
|
258
|
+
concat2,
|
|
259
|
+
/** @readonly */
|
|
255
260
|
first,
|
|
256
261
|
/** @readonly */
|
|
257
262
|
fromArray,
|
package/sequence/test.js
CHANGED
|
@@ -17,7 +17,7 @@ const print = input => {
|
|
|
17
17
|
const big = seq.fromArray([1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 42, 60])
|
|
18
18
|
const list0 = seq.fromArray([0, 1, 2, 3])
|
|
19
19
|
const list1 = seq.flatMap(x => seq.fromArray([x, x * 2, x * 3]))(list0)
|
|
20
|
-
const list2 = seq.concat(list0
|
|
20
|
+
const list2 = seq.concat(list0, list0)
|
|
21
21
|
const list3 = seq.exclusiveScan(sum)(list0)
|
|
22
22
|
const r = seq.find(x => x === 42)(big)
|
|
23
23
|
if (seq.every(x => x > 0)(big) !== true) { throw 'x'}
|
|
@@ -28,7 +28,8 @@ const print = input => {
|
|
|
28
28
|
{
|
|
29
29
|
let x = big
|
|
30
30
|
for (let i = 0; i < 1_000_000; ++i) {
|
|
31
|
-
|
|
31
|
+
// concat() still causes a stack overflow
|
|
32
|
+
x = seq.concat2(seq.empty)(x)
|
|
32
33
|
}
|
|
33
34
|
const r = seq.next(x)
|
|
34
35
|
// print(x)
|
|
@@ -36,7 +37,8 @@ const print = input => {
|
|
|
36
37
|
{
|
|
37
38
|
let x = big
|
|
38
39
|
for (let i = 0; i < 1_000_000; ++i) {
|
|
39
|
-
|
|
40
|
+
// concat() still causes a stack overflow
|
|
41
|
+
x = seq.concat2(x)(seq.one(i))
|
|
40
42
|
}
|
|
41
43
|
const r = seq.next(x)
|
|
42
44
|
// print(x)
|