functionalscript 0.0.271 → 0.0.276

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 CHANGED
@@ -161,7 +161,7 @@ Expressions could fall under these categories:
161
161
  - Relations Operators: `in`, `instanceof`.
162
162
  - Member Operators: `.`, `[]`.
163
163
 
164
- Note: the `.` member operator has prohibitted property names, such as `constructor` and `push`. To access such properties, it's recommeded to use the `Object.getPropertyDescriptor` function.
164
+ Note: the `.` member operator has prohibitted property names, such as `constructor` and `push`. To access such properties, it's recommeded to use the [Object.getOwnPropertyDescriptor](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Reflect/getOwnPropertyDescriptor) function.
165
165
 
166
166
  ## 4. Arrow Functions
167
167
 
@@ -1,12 +1,12 @@
1
1
  const json = require('../../json')
2
2
  const dependencies = require('./dependencies')
3
3
 
4
- /**
4
+ /**
5
5
  * @typedef {{
6
6
  * readonly name: string
7
7
  * readonly version: string
8
8
  * readonly dependencies?: dependencies.DependenciesJson
9
- * }} PackageJson
9
+ * }} PackageJson
10
10
  */
11
11
 
12
12
  /** @type {(j: json.Unknown) => j is PackageJson} */
@@ -18,44 +18,51 @@ const module_ = require("../module")
18
18
  /** @type {(path: string) => readonly string[]} */
19
19
  const split = path => path.split('/')
20
20
 
21
- /** @type {(s: undefined|list.List<string>) => (items: string) => undefined|list.List<string>} */
21
+ /** @typedef {readonly[list.List<string>] | undefined} OptionList */
22
+
23
+ /** @type {(s: OptionList) => (items: string) => OptionList} */
22
24
  const normItemsOp = prior => item => {
23
25
  if (prior === undefined) { return undefined }
26
+ const priorList = prior[0]
24
27
  switch (item) {
25
28
  case '': case '.': { return prior }
26
29
  case '..': {
27
- const result = list.next(prior)
30
+ const result = list.next(priorList)
28
31
  if (result === undefined) { return undefined }
29
- return result.tail
32
+ return [result.tail]
30
33
  }
31
34
  default: {
32
- return list.nonEmpty(item)(prior)
35
+ return [list.nonEmpty(item)(priorList)]
33
36
  }
34
37
  }
35
38
  }
36
39
 
37
- /** @type {(items: list.List<string>) => list.List<string>|undefined} */
38
- const normItems = compose(list.reduce(normItemsOp)([]))(option.map(list.reverse))
40
+ /** @type {(items: list.List<string>) => OptionList} */
41
+ const normItems = items => {
42
+ const result = list.reduce(normItemsOp)([undefined])(items)
43
+ if (result === undefined) { return result }
44
+ return [list.reverse(result[0])]
45
+ }
39
46
 
40
47
  /** @type {(local: string) => (path: string) => LocalPath|undefined} */
41
48
  const parseLocal = local => {
42
- /** @type {(path: string) => readonly[boolean, list.List<string>]} */
49
+ /** @type {(path: string) => readonly[boolean, boolean, list.List<string>]} */
43
50
  const fSeq = path => {
44
51
  const pathSeq = split(path)
45
- switch (list.first(undefined)(pathSeq)) {
46
- case '.': case '..': { return [false, list.flat([split(local), pathSeq])] }
47
- default: { return [true, pathSeq] }
48
- }
52
+ const dir = [undefined, '', '.', '..'].includes(pathSeq[pathSeq.length - 1])
53
+ return /** @type {readonly (string|undefined)[]} */(['.', '..']).includes(list.first(undefined)(pathSeq)) ?
54
+ [false, dir, list.flat([split(local), pathSeq])] :
55
+ [true, dir, pathSeq]
49
56
  }
50
57
  /** @type {(path: string) => LocalPath|undefined} */
51
58
  const f = path => {
52
- const [external, items] = fSeq(path)
59
+ const [external, dir, items] = fSeq(path)
53
60
  const n = normItems(items)
54
61
  if (n === undefined) { return undefined }
55
62
  return {
56
63
  external,
57
- dir: path[path.length - 1] === '/',
58
- items: list.toArray(n)
64
+ dir,
65
+ items: list.toArray(n[0])
59
66
  }
60
67
  }
61
68
  return f
@@ -116,7 +123,13 @@ const parse = packageId => dependencies => local => path => {
116
123
  return parseGlobal(dependencies)(dir)(items)
117
124
  }
118
125
 
119
- /** @typedef {readonly[string, string] | undefined} Result */
126
+ /**
127
+ * @typedef {{
128
+ * readonly package: string
129
+ * readonly file: string
130
+ * readonly source: string
131
+ * }| undefined} Result
132
+ */
120
133
 
121
134
  /**
122
135
  * @type {(packageGet: package_.Get) =>
@@ -133,10 +146,10 @@ const parseAndFind = packageGet => packageId => local => path => {
133
146
  if (p === undefined) { return undefined }
134
147
  const pack = packageGet(p.packageId)
135
148
  if (pack === undefined) { return undefined }
136
- /** @type {(fileId: string) => Result } */
137
- const tryFile = fileId => {
138
- const source = pack.file(fileId)
139
- return source === undefined ? undefined : [fileId, source]
149
+ /** @type {(file: string) => Result } */
150
+ const tryFile = file => {
151
+ const source = pack.file(file)
152
+ return source === undefined ? undefined : { package: p.packageId, file, source }
140
153
  }
141
154
  const file = p.items.join('/')
142
155
  const indexJs = list.join('/')(list.concat(p.items)(['index.js']))
@@ -1,7 +1,10 @@
1
1
  const _ = require('.')
2
+ const { todo } = require('../../dev')
2
3
  const json = require('../../json')
3
4
  const { identity } = require('../../types/function')
5
+ const object = require('../../types/object')
4
6
  const { at } = require('../../types/object')
7
+ const package_ = require('../package')
5
8
 
6
9
  /** @type {(g: json.Unknown|undefined) => string} */
7
10
  const stringify = g => {
@@ -27,7 +30,7 @@ const stringify = g => {
27
30
 
28
31
  {
29
32
  const result = _.parseLocal('a')('')
30
- if (stringify(result) !== '{"external":true,"dir":false,"items":[]}') { throw result }
33
+ if (stringify(result) !== '{"external":true,"dir":true,"items":[]}') { throw result }
31
34
  }
32
35
 
33
36
  {
@@ -45,21 +48,142 @@ const stringify = g => {
45
48
  if (stringify(result) !== '{"external":true,"dir":false,"items":["a","c"]}') { throw result }
46
49
  }
47
50
 
51
+ {
52
+ const result = _.parseLocal('')('./x/..')
53
+ if (stringify(result) !== '{"external":false,"dir":true,"items":[]}') { throw result }
54
+ }
55
+
48
56
  {
49
57
  if (_.parseGlobal(() => undefined)(false)(['a', 'b']) !== undefined) { throw 'error' }
50
58
  if (_.parseGlobal(() => undefined)(false)(['b']) !== undefined) { throw 'error' }
51
59
  if (_.parseGlobal(d => at(d)({ b: 'x' }))(false)(['d']) !== undefined) { throw 'error' }
52
60
  {
53
61
  const result = stringify(_.parseGlobal(d => at(d)({ b: 'x' }))(false)(['b']))
54
- if (result !== '["x",""]') { throw result }
62
+ if (result !== '{"packageId":"x","items":[],"dir":false}') { throw result }
55
63
  }
56
64
  if (_.parseGlobal(d => at(d)({ 'b/r': 'x' }))(false)(['b']) !== undefined) { throw 'error' }
57
65
  {
58
66
  const result = stringify(_.parseGlobal(d => at(d)({ 'b/r': 'x' }))(false)(['b', 'r']))
59
- if (result !== '["x",""]') { throw result }
67
+ if (result !== '{"packageId":"x","items":[],"dir":false}') { throw result }
60
68
  }
61
69
  {
62
70
  const result = stringify(_.parseGlobal(d => at(d)({ 'b/r': 'x' }))(false)(['b', 'r', 'd', 't']))
63
- if (result !== '["x","d/t"]') { throw result }
71
+ if (result !== '{"packageId":"x","items":["d","t"],"dir":false}') { throw result }
72
+ }
73
+ {
74
+ const result = stringify(_.parseGlobal(d => at(d)({ 'b/r': 'x' }))(true)(['b', 'r', 'd', 't']))
75
+ if (result !== '{"packageId":"x","items":["d","t"],"dir":true}') { throw result }
76
+ }
77
+ }
78
+
79
+ {
80
+ /** @type {object.Map<package_.Package>} */
81
+ const packages = {
82
+ '': {
83
+ dependency: () => todo(),
84
+ file: path => at(path)({ 'a/c': 'return "a/c"' }),
85
+ }
86
+ }
87
+ const result = stringify(_.parseAndFind(p => at(p)(packages))('')('a/b')('../c'))
88
+ if (result !== '{"package":"","file":"a/c","source":"return \\"a/c\\""}') { throw result }
89
+ }
90
+
91
+ {
92
+ /** @type {object.Map<package_.Package>} */
93
+ const packages = {
94
+ '': {
95
+ dependency: x => {
96
+ const path = `node_modules/${x}`
97
+ return at(path)(packages) !== undefined ? path : undefined
98
+ },
99
+ file: path => at(path)({
100
+ 'index.js': 'return "index.js"',
101
+ 'x/index.js': 'return "x/index.js"',
102
+ 'x.js': 'return "x.js"',
103
+ })
104
+ },
105
+ 'node_modules/z': {
106
+ dependency: () => todo(),
107
+ file: path => at(path)({ 'a/c/index.js': 'return "a/c"' }),
108
+ }
109
+ }
110
+ {
111
+ const result = stringify(_.parseAndFind(p => at(p)(packages))('')('a/b')('z/a/c'))
112
+ if (result !== '{"package":"node_modules/z","file":"a/c/index.js","source":"return \\"a/c\\""}') { throw result }
113
+ }
114
+ {
115
+ const result = stringify(_.parseAndFind(p => at(p)(packages))('')('a/b')('../..'))
116
+ if (result !== '{"package":"","file":"index.js","source":"return \\"index.js\\""}') { throw result }
117
+ }
118
+ {
119
+ const result = stringify(_.parseAndFind(p => at(p)(packages))('')('')('./x'))
120
+ if (result !== '{"package":"","file":"x.js","source":"return \\"x.js\\""}') { throw result }
121
+ }
122
+ {
123
+ const result = stringify(_.parseAndFind(p => at(p)(packages))('')('')('./x.js'))
124
+ if (result !== '{"package":"","file":"x.js","source":"return \\"x.js\\""}') { throw result }
125
+ }
126
+ {
127
+ const result = stringify(_.parseAndFind(p => at(p)(packages))('')('')('./x/'))
128
+ if (result !== '{"package":"","file":"x/index.js","source":"return \\"x/index.js\\""}') { throw result }
129
+ }
130
+ {
131
+ const result = stringify(_.parseAndFind(p => at(p)(packages))('')('x/a')('../'))
132
+ if (result !== '{"package":"","file":"x/index.js","source":"return \\"x/index.js\\""}') { throw result }
133
+ }
134
+ {
135
+ const result = stringify(_.parseAndFind(p => at(p)(packages))('')('x/a')('..'))
136
+ if (result !== '{"package":"","file":"x/index.js","source":"return \\"x/index.js\\""}') { throw result }
64
137
  }
65
138
  }
139
+
140
+ {
141
+ /** @type {object.Map<package_.Package>} */
142
+ const packages = {
143
+ '': {
144
+ dependency: x => {
145
+ const path = `node_modules/${x}`
146
+ return at(path)(packages) !== undefined ? path : undefined
147
+ },
148
+ file: todo
149
+ },
150
+ 'node_modules/z/a': {
151
+ dependency: () => todo(),
152
+ file: path => at(path)({
153
+ 'c/index.js': 'return "c/index.js"',
154
+ 'c.js': 'return "c.js"'
155
+ }),
156
+ }
157
+ }
158
+ {
159
+ const result = stringify(_.parseAndFind(p => at(p)(packages))('')('a/b')('z/a/c'))
160
+ if (result !== '{"package":"node_modules/z/a","file":"c.js","source":"return \\"c.js\\""}') { throw result }
161
+ }
162
+ {
163
+ const result = stringify(_.parseAndFind(p => at(p)(packages))('')('a/b')('z/a/c/'))
164
+ if (result !== '{"package":"node_modules/z/a","file":"c/index.js","source":"return \\"c/index.js\\""}') { throw result }
165
+ }
166
+ }
167
+
168
+ {
169
+ /** @type {object.Map<package_.Package>} */
170
+ const packages = {
171
+ '': {
172
+ dependency: x => {
173
+ const path = `node_modules/${x}`
174
+ return at(path)(packages) !== undefined ? path : undefined
175
+ },
176
+ file: todo
177
+ },
178
+ 'node_modules/z/a/c': {
179
+ dependency: () => todo(),
180
+ file: path => at(path)({
181
+ '': 'throw',
182
+ '.js': 'throw',
183
+ 'index.js': 'return "a/c"'
184
+ }),
185
+ }
186
+ }
187
+ const result = stringify(_.parseAndFind(p => at(p)(packages))('')('a/b')('z/a/c'))
188
+ if (result !== '{"package":"node_modules/z/a/c","file":"index.js","source":"return \\"a/c\\""}') { throw result }
189
+ }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "functionalscript",
3
- "version": "0.0.271",
3
+ "version": "0.0.276",
4
4
  "description": "FunctionalScript is a functional subset of JavaScript",
5
5
  "main": "index.js",
6
6
  "scripts": {
package/test.js CHANGED
@@ -1,12 +1,14 @@
1
1
  const i = require('./')
2
2
 
3
3
  require('./types/list/test')
4
+ require('./types/array/test')
4
5
  require('./types/btree/test')
5
6
  require('./json/test')
6
7
  require('./types/object/test')
7
8
  require('./io/commonjs/test')
8
9
  require('./commonjs/package/dependencies/test')
9
10
  require('./commonjs/package/test')
11
+ require('./commonjs/path/test')
10
12
 
11
13
  /** @type {() => never} */
12
14
  const assert = () => { throw 'assert' }
@@ -100,12 +102,12 @@ const assert_if = c => { if (c) { throw 'assert_if' } }
100
102
  //const c = o['__defineGetter__']
101
103
  //const c = o['__defineSetter__']
102
104
  //const c = o['__lookupGetter__']
103
- //const c = o['__lookupSetter__']
104
- //const c = o['hasOwnProperty']
105
+ //const c = o['__lookupSetter__']
106
+ //const c = o['hasOwnProperty']
105
107
  //const c = o['isPrototypeOf']
106
108
  //const c = o['propertyIsEnumerable']
107
109
  //const c = o['toString']
108
- const c = o['valueOf']
110
+ const c = o['valueOf']
109
111
  //console.log(c)
110
112
  }
111
113
 
@@ -0,0 +1,94 @@
1
+ const _ = require('.')
2
+ const json = require('../../json')
3
+ const { sort } = require('../object')
4
+
5
+ /** @type {(a: _.Array<json.Unknown>) => string} */
6
+ const stringify = a => json.stringify(sort)(a)
7
+
8
+ {
9
+ const result = stringify([1, 20, 300])
10
+ if (result !== '[1,20,300]') { throw result }
11
+ }
12
+
13
+ {
14
+ const result = _.at(2)([1, 20, 300])
15
+ if (result === undefined) {throw result}
16
+ if (result[0] !== 300) { throw result }
17
+ }
18
+
19
+ {
20
+ const result = _.at(3)([1, 20, 300])
21
+ if (result !== undefined) {throw result}
22
+ }
23
+
24
+ {
25
+ const result = _.first([1, 20, 300])
26
+ if (result !== 1) { throw result }
27
+ }
28
+
29
+ {
30
+ const result = _.first([])
31
+ if (result !== undefined) {throw result}
32
+ }
33
+
34
+ {
35
+ const result = _.last([1, 20, 300])
36
+ if (result !== 300) { throw result }
37
+ }
38
+
39
+ {
40
+ const result = _.last([])
41
+ if (result !== undefined) {throw result}
42
+ }
43
+
44
+ {
45
+ const result = _.head([1, 20, 300])
46
+ if (result === undefined) {throw result}
47
+ const str = stringify(result)
48
+ if (str !== '[1,20]') { throw str }
49
+ }
50
+
51
+ {
52
+ const result = _.head([])
53
+ if (result !== undefined) {throw result}
54
+ }
55
+
56
+ {
57
+ const result = _.tail([1, 20, 300])
58
+ if (result === undefined) {throw result}
59
+ const str = stringify(result)
60
+ if (str !== '[20,300]') { throw str }
61
+ }
62
+
63
+ {
64
+ const result = _.tail([])
65
+ if (result !== undefined) {throw result}
66
+ }
67
+
68
+ {
69
+ const result = _.splitFirst([1, 20, 300])
70
+ if (result === undefined) {throw result}
71
+ const str = stringify(result)
72
+ if (str !== '[1,[20,300]]') { throw str }
73
+ }
74
+
75
+ {
76
+ const result = _.splitFirst([])
77
+ if (result !== undefined) {throw result}
78
+ }
79
+
80
+ {
81
+ const result = _.splitLast([1, 20, 300])
82
+ if (result === undefined) {throw result}
83
+ const str = stringify(result)
84
+ if (str !== '[[1,20],300]') { throw str }
85
+ }
86
+
87
+ {
88
+ const result = _.splitLast([])
89
+ if (result !== undefined) {throw result}
90
+ }
91
+
92
+ module.exports = {
93
+
94
+ }
@@ -58,6 +58,11 @@ const stringify = sequence => json.stringify(sort)(_.toArray(sequence))
58
58
  if (result !== '[]') { throw result }
59
59
  }
60
60
 
61
+ {
62
+ const result = _.find(undefined)(x => x % 2 === 0)([1, 3, 5, 7])
63
+ if (result !== undefined) { throw result }
64
+ }
65
+
61
66
  {
62
67
  const result = _.find(undefined)(x => x % 2 === 0)([1, 2, 3, 4])
63
68
  if (result !== 2) { throw result }
@@ -119,6 +124,11 @@ const stringify = sequence => json.stringify(sort)(_.toArray(sequence))
119
124
  if (result !== '') { throw result }
120
125
  }
121
126
 
127
+ {
128
+ const result = _.join(' ')(['hello', 'world', '!'])
129
+ if (result !== 'hello world !') { throw result }
130
+ }
131
+
122
132
  {
123
133
  const result = stringify(_.entries([]))
124
134
  if (result !== '[]') { throw result }
@@ -329,7 +339,7 @@ const stress = () => {
329
339
  }
330
340
  }
331
341
 
332
- stress()
342
+ // stress()
333
343
 
334
344
  module.exports = {
335
345