functionalscript 0.0.205 → 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 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)(srcObject[name]) }
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
- /** @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)
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'])({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
+ }
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.205",
3
+ "version": "0.0.208",
4
4
  "description": "FunctionalScript is a functional subset of JavaScript",
5
5
  "main": "index.js",
6
6
  "scripts": {
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
  }