functionalscript 0.0.203 → 0.0.210

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/function/index.js CHANGED
@@ -5,7 +5,7 @@
5
5
  */
6
6
 
7
7
  /** @type {<X, O>(f: Func<X, O>) => <I>(g: Func<I, X>) => Func<I, O>} */
8
- const combine = f => g => x => f(g(x))
8
+ const compose = f => g => x => f(g(x))
9
9
 
10
10
  /** @type {<T>(value: T) => T} */
11
11
  const id = value => value
@@ -14,5 +14,5 @@ module.exports = {
14
14
  /** @readonly */
15
15
  id,
16
16
  /** @readonly */
17
- combine,
17
+ compose,
18
18
  }
package/json/index.js CHANGED
@@ -1,8 +1,8 @@
1
1
  const seq = require('../sequence')
2
- const map = require('../map')
3
2
  const op = require('../sequence/operator')
4
3
  const object = require('../object')
5
4
  const array = require('../sequence/array')
5
+ const { compose: combine } = require('../function')
6
6
 
7
7
  /**
8
8
  * @typedef {{
@@ -22,9 +22,9 @@ const addProperty = value => {
22
22
  if (result === undefined) { return value }
23
23
  const srcObject = (src === undefined || src === null || typeof src !== 'object' || src instanceof Array) ? {} : src
24
24
  const [name, tail] = result
25
- return { ...srcObject, [name]: f(tail)(srcObject[name]) }
25
+ return { ...srcObject, [name]: f(tail)(object.at(name)(srcObject)) }
26
26
  }
27
- return path => f(array.sequence(path))
27
+ return combine(f)(array.sequence)
28
28
  }
29
29
 
30
30
  /** @type {(_: string) => seq.Sequence<string>} */
@@ -42,12 +42,6 @@ const falseSerialize = seq.list('false')
42
42
  /** @type {(_: boolean) => seq.Sequence<string>} */
43
43
  const boolSerialize = value => value ? trueSerialize : falseSerialize
44
44
 
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
45
  const colon = seq.list(':')
52
46
  const comma = seq.list(',')
53
47
 
@@ -74,33 +68,46 @@ const objectList = list('{')('}')
74
68
 
75
69
  const arrayList = list('[')(']')
76
70
 
77
- /** @type {(object: Object) => seq.Sequence<string>} */
78
- const objectSerialize = input => {
79
- const _0 = object.entries(input)
80
- const _1 = map.fromEntries(_0).entries
81
- const _2 = seq.map(propertySerialize)(_1)
82
- return objectList(_2)
83
- }
84
-
85
- /** @type {(input: Array) => seq.Sequence<string>} */
86
- const arraySerialize = input => {
87
- const _0 = array.sequence(input)
88
- const _1 = seq.map(serialize)(_0)
89
- return arrayList(_1)
90
- }
91
-
92
- /** @type {(value: Json) => seq.Sequence<string>} */
93
- const serialize = value => {
94
- switch (typeof value) {
95
- case 'boolean': { return boolSerialize(value) }
96
- case 'number': { return numberSerialize(value) }
97
- case 'string': { return stringSerialize(value) }
98
- default: {
99
- if (value === null) { return nullSerialize }
100
- if (value instanceof Array) { return arraySerialize(value) }
101
- return objectSerialize(value)
71
+ /** @typedef {object.Entry<Json>} Entry*/
72
+
73
+ /** @typedef {(seq.Sequence<Entry>)} Entries */
74
+
75
+ /** @typedef {(entries: Entries) => Entries} MapEntries */
76
+
77
+ /** @type {(mapEntries: MapEntries) => (value: Json) => seq.Sequence<string>} */
78
+ const serialize = sort => {
79
+ /** @type {(kv: readonly[string, Json]) => seq.Sequence<string>} */
80
+ const propertySerialize = ([k, v]) => seq.concat(
81
+ stringSerialize(k),
82
+ colon,
83
+ f(v))
84
+ /** @type {(object: Object) => seq.Sequence<string>} */
85
+ const objectSerialize = input => {
86
+ const entries = object.entries(input)
87
+ const sortedEntries = sort(entries)
88
+ const serializedEntries = seq.map(propertySerialize)(sortedEntries)
89
+ return objectList(serializedEntries)
90
+ }
91
+ /** @type {(input: Array) => seq.Sequence<string>} */
92
+ const arraySerialize = input => {
93
+ const sequence = array.sequence(input)
94
+ const serializedEntries = seq.map(f)(sequence)
95
+ return arrayList(serializedEntries)
96
+ }
97
+ /** @type {(value: Json) => seq.Sequence < string >} */
98
+ const f = value => {
99
+ switch (typeof value) {
100
+ case 'boolean': { return boolSerialize(value) }
101
+ case 'number': { return numberSerialize(value) }
102
+ case 'string': { return stringSerialize(value) }
103
+ default: {
104
+ if (value === null) { return nullSerialize }
105
+ if (value instanceof Array) { return arraySerialize(value) }
106
+ return objectSerialize(value)
107
+ }
102
108
  }
103
109
  }
110
+ return f
104
111
  }
105
112
 
106
113
  /**
@@ -109,9 +116,9 @@ const serialize = value => {
109
116
  * The standard `JSON.stringify` rules determines by
110
117
  * https://262.ecma-international.org/6.0/#sec-ordinary-object-internal-methods-and-internal-slots-ownpropertykeys
111
118
  *
112
- * @type {(value: Json) => string}
119
+ * @type {(mapEntries: MapEntries) => (value: Json) => string}
113
120
  */
114
- const stringify = value => seq.join('')(serialize(value))
121
+ const stringify = sort => value => seq.join('')(serialize(sort)(value))
115
122
 
116
123
  /** @type {(value: string) => Json} */
117
124
  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'])({c:[],b:12}))
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
+ }
@@ -5,26 +5,31 @@
5
5
  ```js
6
6
  /** @typedef {(packageName: string) => PackageMap|Package|undefined} PackageMap */
7
7
 
8
- /**
9
- * @typedef {{
10
- * readonly id: string,
11
- * readonly packages: PackageMap,
12
- * readonly files: (fileName: string) => string|undefined,
13
- * }} Package
8
+ /**
9
+ * @typedef {readonly[
10
+ * string,
11
+ * PackageMap,
12
+ * (fileName: string) => string|undefined
13
+ * ]} Package
14
14
  */
15
15
  ```
16
16
 
17
17
  ## Runner IO
18
18
 
19
+ The main target of this design is to simplify `RunnerIo` as much as possible.
20
+
19
21
  ```js
20
- /**
21
- * @typedef {(require: Require<T>) => (info: T) => (source: string) => readonly[Result<unknown, Error>, T]} RunnerIo
22
+ /**
23
+ * @template T
24
+ * @typedef {readonly[Result<unknown, Error>, Require<T>]} RunnerResult
22
25
  */
23
- ```
24
26
 
25
- ```js
27
+ /** @typedef {<T>(require: Require<T>) => (source: string) => RunnerResult<T>} RunnerIo */
28
+
26
29
  /**
27
30
  * @template T
28
- * @typedef {(info: T) => (module: string) => readonly[Result<unknown, Error>, T]} Require<T>
31
+ * @typedef {readonly[(path: string) => RunnerResult<T>, T]} Require<T>
29
32
  */
30
33
  ```
34
+
35
+ `Require` is using a `Package` and it contains also `RunnerIo` to provide sources also it contains
@@ -1,5 +1,5 @@
1
1
  const array = require('../sequence/array')
2
- const { combine } = require('../function')
2
+ const { compose: combine } = require('../function')
3
3
  const option = require('../option')
4
4
  const { head, last, splitLast, splitFirst } = array
5
5
  const iter = require('../sequence/iterable')
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
- * }} Object_
9
+ * }} Map
9
10
  */
10
11
 
11
- /** @type {<T>(object: Object_<T>) => seq.Sequence<readonly[string, T]>} */
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
@@ -0,0 +1,13 @@
1
+ const _ = require('.')
2
+
3
+ {
4
+ const a = {}
5
+ const value = _.at('constructor')(a)
6
+ if (value !== undefined) { throw value }
7
+ }
8
+
9
+ {
10
+ const a = { constructor: 42}
11
+ const value = _.at('constructor')(a)
12
+ if (value !== 42) { throw value }
13
+ }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "functionalscript",
3
- "version": "0.0.203",
3
+ "version": "0.0.210",
4
4
  "description": "FunctionalScript is a functional subset of JavaScript",
5
5
  "main": "index.js",
6
6
  "scripts": {
@@ -0,0 +1,17 @@
1
+ /**
2
+ * @template T
3
+ * @typedef {readonly['ok', T]} Ok
4
+ */
5
+
6
+ /**
7
+ * @template E
8
+ * @typedef {readonly['error', E]} Error
9
+ */
10
+
11
+ /**
12
+ * @template T
13
+ * @template E
14
+ * @typedef {Ok<T>|Error<E>} Result
15
+ */
16
+
17
+ module.exports = {}
@@ -1,4 +1,4 @@
1
- const { combine } = require('../../function')
1
+ const { compose: combine } = require('../../function')
2
2
  const seq = require('../operator')
3
3
 
4
4
  /**
package/sequence/index.js CHANGED
@@ -1,5 +1,5 @@
1
1
  const seqOp = require('./operator')
2
- const { combine } = require('../function')
2
+ const { compose: combine } = require('../function')
3
3
  const op = require('../function/operator')
4
4
  const { logicalNot, strictEqual } = require('../function/operator')
5
5
 
@@ -1,5 +1,5 @@
1
1
  const { iterable } = require('..')
2
- const { combine } = require('../../function')
2
+ const { compose: combine } = require('../../function')
3
3
  const seq = require('../operator')
4
4
 
5
5
  /** @type {<T>(a: Iterable<T>) => (b: Iterable<T>) => Iterable<T>} */
@@ -1,5 +1,5 @@
1
1
  const i = require('.')
2
- const { combine } = require('../../function')
2
+ const { compose: combine } = require('../../function')
3
3
 
4
4
  {
5
5
  const r = i.sum([120, 300, 42])
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
  }