functionalscript 0.0.202 → 0.0.208
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 +42 -34
- package/json/test.js +22 -3
- package/module-manager/README.md +26 -5
- package/object/index.js +18 -2
- package/object/test.js +13 -0
- package/package.json +1 -1
- package/result/index.js +17 -0
- package/test.js +8 -1
package/json/index.js
CHANGED
|
@@ -3,6 +3,7 @@ const map = require('../map')
|
|
|
3
3
|
const op = require('../sequence/operator')
|
|
4
4
|
const object = require('../object')
|
|
5
5
|
const array = require('../sequence/array')
|
|
6
|
+
const { id } = require('../function')
|
|
6
7
|
|
|
7
8
|
/**
|
|
8
9
|
* @typedef {{
|
|
@@ -22,7 +23,7 @@ const addProperty = value => {
|
|
|
22
23
|
if (result === undefined) { return value }
|
|
23
24
|
const srcObject = (src === undefined || src === null || typeof src !== 'object' || src instanceof Array) ? {} : src
|
|
24
25
|
const [name, tail] = result
|
|
25
|
-
return { ...srcObject, [name]: f(tail)(
|
|
26
|
+
return { ...srcObject, [name]: f(tail)(object.at(name)(srcObject)) }
|
|
26
27
|
}
|
|
27
28
|
return path => f(array.sequence(path))
|
|
28
29
|
}
|
|
@@ -42,12 +43,6 @@ const falseSerialize = seq.list('false')
|
|
|
42
43
|
/** @type {(_: boolean) => seq.Sequence<string>} */
|
|
43
44
|
const boolSerialize = value => value ? trueSerialize : falseSerialize
|
|
44
45
|
|
|
45
|
-
/** @type {(kv: readonly[string, Json]) => seq.Sequence<string>} */
|
|
46
|
-
const propertySerialize = ([k, v]) => seq.concat(
|
|
47
|
-
stringSerialize(k),
|
|
48
|
-
colon,
|
|
49
|
-
serialize(v))
|
|
50
|
-
|
|
51
46
|
const colon = seq.list(':')
|
|
52
47
|
const comma = seq.list(',')
|
|
53
48
|
|
|
@@ -74,33 +69,46 @@ const objectList = list('{')('}')
|
|
|
74
69
|
|
|
75
70
|
const arrayList = list('[')(']')
|
|
76
71
|
|
|
77
|
-
/** @
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
}
|
|
84
|
-
|
|
85
|
-
/** @type {(
|
|
86
|
-
const
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
}
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
const
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
72
|
+
/** @typedef {object.Entry<Json>} Entry*/
|
|
73
|
+
|
|
74
|
+
/** @typedef {(seq.Sequence<Entry>)} Entries */
|
|
75
|
+
|
|
76
|
+
/** @typedef {(entries: Entries) => Entries} MapEntries */
|
|
77
|
+
|
|
78
|
+
/** @type {(mapEntries: MapEntries) => (value: Json) => seq.Sequence<string>} */
|
|
79
|
+
const serialize = sort => {
|
|
80
|
+
/** @type {(kv: readonly[string, Json]) => seq.Sequence<string>} */
|
|
81
|
+
const propertySerialize = ([k, v]) => seq.concat(
|
|
82
|
+
stringSerialize(k),
|
|
83
|
+
colon,
|
|
84
|
+
f(v))
|
|
85
|
+
/** @type {(object: Object) => seq.Sequence<string>} */
|
|
86
|
+
const objectSerialize = input => {
|
|
87
|
+
const _0 = object.entries(input)
|
|
88
|
+
const _1 = sort(_0)
|
|
89
|
+
const _2 = seq.map(propertySerialize)(_1)
|
|
90
|
+
return objectList(_2)
|
|
91
|
+
}
|
|
92
|
+
/** @type {(input: Array) => seq.Sequence<string>} */
|
|
93
|
+
const arraySerialize = input => {
|
|
94
|
+
const _0 = array.sequence(input)
|
|
95
|
+
const _1 = seq.map(f)(_0)
|
|
96
|
+
return arrayList(_1)
|
|
97
|
+
}
|
|
98
|
+
/** @type {(value: Json) => seq.Sequence < string >} */
|
|
99
|
+
const f = value => {
|
|
100
|
+
switch (typeof value) {
|
|
101
|
+
case 'boolean': { return boolSerialize(value) }
|
|
102
|
+
case 'number': { return numberSerialize(value) }
|
|
103
|
+
case 'string': { return stringSerialize(value) }
|
|
104
|
+
default: {
|
|
105
|
+
if (value === null) { return nullSerialize }
|
|
106
|
+
if (value instanceof Array) { return arraySerialize(value) }
|
|
107
|
+
return objectSerialize(value)
|
|
108
|
+
}
|
|
102
109
|
}
|
|
103
110
|
}
|
|
111
|
+
return f
|
|
104
112
|
}
|
|
105
113
|
|
|
106
114
|
/**
|
|
@@ -109,9 +117,9 @@ const serialize = value => {
|
|
|
109
117
|
* The standard `JSON.stringify` rules determines by
|
|
110
118
|
* https://262.ecma-international.org/6.0/#sec-ordinary-object-internal-methods-and-internal-slots-ownpropertykeys
|
|
111
119
|
*
|
|
112
|
-
* @type {(value: Json) => string}
|
|
120
|
+
* @type {(mapEntries: MapEntries) => (value: Json) => string}
|
|
113
121
|
*/
|
|
114
|
-
const stringify = value => seq.join('')(serialize(value))
|
|
122
|
+
const stringify = sort => value => seq.join('')(serialize(sort)(value))
|
|
115
123
|
|
|
116
124
|
/** @type {(value: string) => Json} */
|
|
117
125
|
const parse = value => JSON.parse(value)
|
package/json/test.js
CHANGED
|
@@ -1,20 +1,39 @@
|
|
|
1
1
|
const json = require('.')
|
|
2
|
+
const { sort } = require('../object')
|
|
3
|
+
const { id } = require('../function')
|
|
2
4
|
|
|
3
5
|
if (json.addProperty("Hello")([])({}) !== "Hello") { throw 'error' }
|
|
4
6
|
|
|
5
7
|
{
|
|
6
|
-
const x = json.stringify(json.addProperty("Hello")(['a'])({}))
|
|
8
|
+
const x = json.stringify(sort)(json.addProperty("Hello")(['a'])({}))
|
|
7
9
|
if (x !== '{"a":"Hello"}') { throw x }
|
|
8
10
|
}
|
|
9
11
|
|
|
10
12
|
{
|
|
11
|
-
const x = json.stringify(json.addProperty("Hello")(['a'])({
|
|
13
|
+
const x = json.stringify(id)(json.addProperty("Hello")(['a'])({}))
|
|
14
|
+
if (x !== '{"a":"Hello"}') { throw x }
|
|
15
|
+
}
|
|
16
|
+
|
|
17
|
+
{
|
|
18
|
+
const x = json.stringify(sort)(json.addProperty("Hello")(['a'])({c:[],b:12}))
|
|
12
19
|
if (x !== '{"a":"Hello","b":12,"c":[]}') { throw x }
|
|
13
20
|
}
|
|
14
21
|
|
|
22
|
+
{
|
|
23
|
+
const x = json.stringify(id)(json.addProperty("Hello")(['a'])({ c: [], b: 12 }))
|
|
24
|
+
if (x !== '{"c":[],"b":12,"a":"Hello"}') { throw x }
|
|
25
|
+
}
|
|
26
|
+
|
|
15
27
|
{
|
|
16
28
|
const _0 = { a: { y: [24] }, c: [], b: 12 }
|
|
17
29
|
const _1 = json.addProperty("Hello")(['a', 'x'])(_0)
|
|
18
|
-
const _2 = json.stringify(_1)
|
|
30
|
+
const _2 = json.stringify(sort)(_1)
|
|
19
31
|
if (_2 !== '{"a":{"x":"Hello","y":[24]},"b":12,"c":[]}') { throw _2 }
|
|
20
32
|
}
|
|
33
|
+
|
|
34
|
+
{
|
|
35
|
+
const _0 = { a: { y: [24] }, c: [], b: 12 }
|
|
36
|
+
const _1 = json.addProperty("Hello")(['a', 'x'])(_0)
|
|
37
|
+
const _2 = json.stringify(id)(_1)
|
|
38
|
+
if (_2 !== '{"a":{"y":[24],"x":"Hello"},"c":[],"b":12}') { throw _2 }
|
|
39
|
+
}
|
package/module-manager/README.md
CHANGED
|
@@ -1,14 +1,35 @@
|
|
|
1
1
|
# Module Manager
|
|
2
2
|
|
|
3
|
-
##
|
|
3
|
+
## Module Provider
|
|
4
4
|
|
|
5
5
|
```js
|
|
6
6
|
/** @typedef {(packageName: string) => PackageMap|Package|undefined} PackageMap */
|
|
7
7
|
|
|
8
|
-
/**
|
|
8
|
+
/**
|
|
9
9
|
* @typedef {readonly[
|
|
10
|
-
*
|
|
11
|
-
*
|
|
12
|
-
*
|
|
10
|
+
* string,
|
|
11
|
+
* PackageMap,
|
|
12
|
+
* (fileName: string) => string|undefined
|
|
13
|
+
* ]} Package
|
|
13
14
|
*/
|
|
14
15
|
```
|
|
16
|
+
|
|
17
|
+
## Runner IO
|
|
18
|
+
|
|
19
|
+
The main target of this design is to simplify `RunnerIo` as much as possible.
|
|
20
|
+
|
|
21
|
+
```js
|
|
22
|
+
/**
|
|
23
|
+
* @template T
|
|
24
|
+
* @typedef {readonly[Result<unknown, Error>, Require<T>]} RunnerResult
|
|
25
|
+
*/
|
|
26
|
+
|
|
27
|
+
/** @typedef {<T>(require: Require<T>) => (source: string) => RunnerResult<T>} RunnerIo */
|
|
28
|
+
|
|
29
|
+
/**
|
|
30
|
+
* @template T
|
|
31
|
+
* @typedef {readonly[(path: string) => RunnerResult<T>, T]} Require<T>
|
|
32
|
+
*/
|
|
33
|
+
```
|
|
34
|
+
|
|
35
|
+
`Require` is using a `Package` and it contains also `RunnerIo` to provide sources also it contains
|
package/object/index.js
CHANGED
|
@@ -1,17 +1,33 @@
|
|
|
1
1
|
const array = require('../sequence/array')
|
|
2
2
|
const seq = require('../sequence')
|
|
3
|
+
const map = require('../map')
|
|
3
4
|
|
|
4
5
|
/**
|
|
5
6
|
* @template T
|
|
6
7
|
* @typedef {{
|
|
7
8
|
* readonly [k in string]: T
|
|
8
|
-
* }}
|
|
9
|
+
* }} Map
|
|
9
10
|
*/
|
|
10
11
|
|
|
11
|
-
/**
|
|
12
|
+
/**
|
|
13
|
+
* @template T
|
|
14
|
+
* @typedef {readonly[string, T]} Entry
|
|
15
|
+
*/
|
|
16
|
+
|
|
17
|
+
/** @type {<T>(object: Map<T>) => seq.Sequence<Entry<T>>} */
|
|
12
18
|
const entries = object => array.sequence(Object.entries(object))
|
|
13
19
|
|
|
20
|
+
/** @type {(name: string) => <T>(object: Map<T>) => T|undefined} */
|
|
21
|
+
const at = name => object => Object.getOwnPropertyDescriptor(object, name)?.value
|
|
22
|
+
|
|
23
|
+
/** @type {<T>(entries: seq.Sequence<Entry<T>>) => seq.Sequence<Entry<T>>} */
|
|
24
|
+
const sort = entries => map.fromEntries(entries).entries
|
|
25
|
+
|
|
14
26
|
module.exports = {
|
|
15
27
|
/** @readonly */
|
|
16
28
|
entries,
|
|
29
|
+
/** @readonly */
|
|
30
|
+
at,
|
|
31
|
+
/** @readonly */
|
|
32
|
+
sort,
|
|
17
33
|
}
|
package/object/test.js
ADDED
package/package.json
CHANGED
package/result/index.js
ADDED
package/test.js
CHANGED
|
@@ -6,6 +6,7 @@ require('./sequence/iterable/test')
|
|
|
6
6
|
require('./sequence/asyncIterable/test')
|
|
7
7
|
require('./module-manager/test')
|
|
8
8
|
require('./json/test')
|
|
9
|
+
require('./object/test')
|
|
9
10
|
|
|
10
11
|
/** @type {() => never} */
|
|
11
12
|
const assert = () => { throw 'assert' }
|
|
@@ -104,6 +105,12 @@ const assert_if = c => { if (c) { throw 'assert_if' } }
|
|
|
104
105
|
//const c = o['isPrototypeOf']
|
|
105
106
|
//const c = o['propertyIsEnumerable']
|
|
106
107
|
//const c = o['toString']
|
|
107
|
-
const c = o['valueOf']
|
|
108
|
+
const c = o['valueOf']
|
|
108
109
|
console.log(c)
|
|
110
|
+
}
|
|
111
|
+
|
|
112
|
+
{
|
|
113
|
+
const x = { 'a': 12 }
|
|
114
|
+
const c = Object.getOwnPropertyDescriptor(x, 'constructor')
|
|
115
|
+
const a = Object.getOwnPropertyDescriptor(x, 'a')
|
|
109
116
|
}
|