functionalscript 0.0.432 → 0.0.433

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/dev/module.mjs ADDED
@@ -0,0 +1,155 @@
1
+ /**
2
+ * @typedef {{
3
+ * readonly withFileTypes: true
4
+ * }} Options
5
+ */
6
+
7
+ /**
8
+ * @typedef {{
9
+ * readonly name: string
10
+ * readonly isDirectory: () => boolean
11
+ * }} Dirent
12
+ */
13
+
14
+ /**
15
+ * @typedef {{
16
+ * readonly readdir: (path: string, options: Options) => Promise<readonly Dirent[]>
17
+ * readonly readFile: (path: string, options: 'utf8') => Promise<string>
18
+ * }} FsPromises
19
+ */
20
+
21
+ /**
22
+ * @typedef {{
23
+ * [k in string]?: MutableModule
24
+ * }} MutableDependencyMap
25
+ */
26
+
27
+ /**
28
+ * @typedef {{
29
+ * dependencyMap: MutableDependencyMap
30
+ * exports?: unknown
31
+ * }} MutableModule
32
+ */
33
+
34
+ /**
35
+ * @typedef {{
36
+ * readonly dependencyMap: DependencyMap
37
+ * readonly exports?: unknown
38
+ * }} Module
39
+ */
40
+
41
+ /**
42
+ * @typedef {{
43
+ * readonly[k in string]?: Module
44
+ * }} DependencyMap
45
+ */
46
+
47
+ /** @typedef {(name: string) => unknown} Require */
48
+
49
+ /**
50
+ * @typedef {{
51
+ * readonly[k in string]: Function
52
+ * }} FunctionMap
53
+ */
54
+
55
+ /**
56
+ * @template T
57
+ * @typedef {readonly[string, T]} Entry
58
+ */
59
+
60
+ /** @type {(a: Entry<Function>, b: Entry<Function>) => number} */
61
+ const cmp = ([a], [b]) => a < b ? -1 : a > b ? 1 : 0
62
+
63
+ /**
64
+ * @typedef {{
65
+ * [k in string]: MutableModule
66
+ * }} MutableModuleMap
67
+ */
68
+
69
+ /**
70
+ * @typedef {{
71
+ * readonly[k in string]: Module
72
+ * }} ModuleMap
73
+ */
74
+
75
+ /** @type {(v: readonly string[]) => (dif: number) => readonly string[]} */
76
+ const remove_tail = v => dif => v.slice(0, v.length - dif)
77
+
78
+ /** @type {any} */
79
+ const self = globalThis
80
+
81
+ export const loadModuleMap = async () => {
82
+ /** @type {FsPromises} */
83
+ const { readdir, readFile } = await import(self.Deno ? 'https://deno.land/std/node/fs/promises.ts' : 'node:fs/promises')
84
+
85
+ /** @type {() => Promise<FunctionMap>} */
86
+ const load = async () => {
87
+ /** @type {(readonly[string, Function])[]} */
88
+ const map = []
89
+ /** @type {(path: string) => Promise<void>} */
90
+ const f = async p => {
91
+ for (const i of await readdir(p, { withFileTypes: true })) {
92
+ const { name } = i
93
+ if (!name.startsWith('.')) {
94
+ const file = `${p}/${name}`
95
+ if (i.isDirectory()) {
96
+ await f(file)
97
+ } else if (name.endsWith('.f.cjs')) {
98
+ const source = await readFile(file, 'utf8')
99
+ map.push([file, Function('module', 'require', `"use strict";${source}`)])
100
+ }
101
+ }
102
+ }
103
+ }
104
+ await f('.')
105
+ map.sort(cmp)
106
+ return Object.fromEntries(map)
107
+ }
108
+
109
+ const map = await load()
110
+
111
+ /** @type {() => ModuleMap} */
112
+ const build = () => {
113
+ /** @type {MutableModuleMap} */
114
+ const d = {}
115
+ /** @type {(base: readonly string[]) => (k: string) => readonly[string, MutableModule]} */
116
+ const getModule = base => k => {
117
+ const relativePath = k.split('/')
118
+ const dif = relativePath.filter(v => v === '..').length
119
+ const path = [remove_tail(base)(dif), relativePath.filter(v => !['..', '.'].includes(v))]
120
+ .flat()
121
+ const pathStr = path.join('/')
122
+ {
123
+ const module = d[pathStr]
124
+ if (module !== undefined) {
125
+ return [pathStr, module]
126
+ }
127
+ }
128
+ {
129
+ /** @type {MutableDependencyMap} */
130
+ const dependencyMap = {}
131
+ /** @type {MutableModule} */
132
+ const module = { dependencyMap }
133
+ const get = getModule(remove_tail(path)(1))
134
+ /** @type {(s: string) => unknown} */
135
+ const newReq = s => {
136
+ const [p, result] = get(s)
137
+ dependencyMap[p] = result
138
+ return result.exports
139
+ }
140
+ map[pathStr](module, newReq)
141
+ d[pathStr] = module
142
+ return [pathStr, module]
143
+ }
144
+ }
145
+ {
146
+ const get = getModule(['.'])
147
+ for (const k of Object.keys(map)) {
148
+ get(k)
149
+ }
150
+ }
151
+ return d
152
+ }
153
+
154
+ return build()
155
+ }
@@ -34,6 +34,17 @@ const { fold } = list
34
34
  * }} Input
35
35
  */
36
36
 
37
+ /**
38
+ * https://en.wikipedia.org/wiki/ANSI_escape_code#SGR
39
+ *
40
+ * @type {(c: number) => string}
41
+ */
42
+ const sgr = c => `\x1b[${c.toString()}m`
43
+
44
+ const reset = sgr(0)
45
+
46
+ const fgGreen = sgr(32)
47
+
37
48
  /** @type {(s: string) => boolean} */
38
49
  const isTest = s => s.endsWith('test.f.cjs')
39
50
 
@@ -47,7 +58,7 @@ const main = ({moduleMap, log, state}) => {
47
58
  case 'function': {
48
59
  if (v.length === 0) {
49
60
  const r = v()
50
- state = log(`${i}() passed`)(state)
61
+ state = log(`${i}() ${fgGreen}ok${reset}`)(state)
51
62
  state = next(r)(state)
52
63
  }
53
64
  break
package/dev/test.mjs CHANGED
@@ -1,162 +1,8 @@
1
- /**
2
- * @typedef {{
3
- * readonly withFileTypes: true
4
- * }} Options
5
- */
6
-
7
- /**
8
- * @typedef {{
9
- * readonly name: string
10
- * readonly isDirectory: () => boolean
11
- * }} Dirent
12
- */
13
-
14
- /**
15
- * @typedef {{
16
- * readonly readdir: (path: string, options: Options) => Promise<readonly Dirent[]>
17
- * readonly readFile: (path: string, options: 'utf8') => Promise<string>
18
- * }} FsPromises
19
- */
20
-
21
- /**
22
- * @typedef {{
23
- * [k in string]?: MutableModule
24
- * }} MutableDependencyMap
25
- */
26
-
27
- /**
28
- * @typedef {{
29
- * dependencyMap: MutableDependencyMap
30
- * exports?: unknown
31
- * }} MutableModule
32
- */
33
-
34
- /**
35
- * @typedef {{
36
- * readonly dependencyMap: DependencyMap
37
- * readonly exports?: unknown
38
- * }} Module
39
- */
40
-
41
- /**
42
- * @typedef {{
43
- * readonly[k in string]?: Module
44
- * }} DependencyMap
45
- */
46
-
47
- /** @typedef {(name: string) => unknown} Require */
48
-
49
- /**
50
- * @typedef {{
51
- * readonly[k in string]: Function
52
- * }} FunctionMap
53
- */
54
-
55
- /**
56
- * @template T
57
- * @typedef {readonly[string, T]} Entry
58
- */
59
-
60
- /** @type {(a: Entry<Function>, b: Entry<Function>) => number} */
61
- const cmp = ([a], [b]) => a < b ? -1 : a > b ? 1 : 0
62
-
63
- /**
64
- * @typedef {{
65
- * [k in string]: MutableModule
66
- * }} MutableModuleMap
67
- */
68
-
69
- /**
70
- * @typedef {{
71
- * readonly[k in string]: Module
72
- * }} ModuleMap
73
- */
74
-
75
- /** @type {(v: readonly string[]) => (dif: number) => readonly string[]} */
76
- const remove_tail = v => dif => v.slice(0, v.length - dif)
77
-
78
- /** @type {any} */
79
- const self = globalThis
80
-
81
- const boot = async() => {
82
- /** @type {FsPromises} */
83
- const { readdir, readFile } = await import(self.Deno ? 'https://deno.land/std/node/fs/promises.ts' : 'node:fs/promises')
84
-
85
- /** @type {() => Promise<FunctionMap>} */
86
- const load = async () => {
87
- /** @type {(readonly[string, Function])[]} */
88
- const map = []
89
- /** @type {(path: string) => Promise<void>} */
90
- const f = async p => {
91
- for (const i of await readdir(p, { withFileTypes: true })) {
92
- const { name } = i
93
- if (!name.startsWith('.')) {
94
- const file = `${p}/${name}`
95
- if (i.isDirectory()) {
96
- await f(file)
97
- } else if (name.endsWith('.f.cjs')) {
98
- const source = await readFile(file, 'utf8')
99
- map.push([file, Function('module', 'require', `"use strict";${source}`)])
100
- }
101
- }
102
- }
103
- }
104
- await f('.')
105
- map.sort(cmp)
106
- return Object.fromEntries(map)
107
- }
108
-
109
- const map = await load()
110
-
111
- /** @type {() => ModuleMap} */
112
- const build = () => {
113
- /** @type {MutableModuleMap} */
114
- const d = {}
115
- /** @type {(base: readonly string[]) => (k: string) => readonly[string, MutableModule]} */
116
- const getModule = base => k => {
117
- const relativePath = k.split('/')
118
- const dif = relativePath.filter(v => v === '..').length
119
- const path = [remove_tail(base)(dif), relativePath.filter(v => !['..', '.'].includes(v))]
120
- .flat()
121
- const pathStr = path.join('/')
122
- {
123
- const module = d[pathStr]
124
- if (module !== undefined) {
125
- return [pathStr, module]
126
- }
127
- }
128
- {
129
- /** @type {MutableDependencyMap} */
130
- const dependencyMap = {}
131
- /** @type {MutableModule} */
132
- const module = { dependencyMap }
133
- const get = getModule(remove_tail(path)(1))
134
- /** @type {(s: string) => unknown} */
135
- const newReq = s => {
136
- const [p, result] = get(s)
137
- dependencyMap[p] = result
138
- return result.exports
139
- }
140
- map[pathStr](module, newReq)
141
- d[pathStr] = module
142
- return [pathStr, module]
143
- }
144
- }
145
- {
146
- const get = getModule(['.'])
147
- for (const k of Object.keys(map)) {
148
- get(k)
149
- }
150
- }
151
- return d
152
- }
153
-
154
- return build()
155
- }
1
+ import { loadModuleMap } from './module.mjs'
156
2
 
157
3
  // test runner.
158
4
  const main = async() => {
159
- const moduleMap = await boot()
5
+ const moduleMap = await loadModuleMap()
160
6
 
161
7
  /** @type {(s: string) => (_: undefined) => undefined} */
162
8
  const log = s => state => {
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "functionalscript",
3
- "version": "0.0.432",
3
+ "version": "0.0.433",
4
4
  "description": "FunctionalScript is a functional subset of JavaScript",
5
5
  "main": "module.f.cjs",
6
6
  "scripts": {
@@ -7,7 +7,7 @@
7
7
 
8
8
  /**
9
9
  * @template I,O
10
- * @typedef {Binary<I, O, O>} FoldT
10
+ * @typedef {Binary<I, O, O>} Fold
11
11
  */
12
12
 
13
13
  /** @type {(separator: string) => Reduce<string>} */
@@ -49,7 +49,7 @@ const stateScanToScan = op => prior => i => {
49
49
  return [o, stateScanToScan(op)(s)]
50
50
  }
51
51
 
52
- /** @type {<I, O>(fold: FoldT<I, O>) => (prior: O) => Scan<I, O>} */
52
+ /** @type {<I, O>(fold: Fold<I, O>) => (prior: O) => Scan<I, O>} */
53
53
  const foldToScan = fold => prior => i => {
54
54
  const result = fold(i)(prior)
55
55
  return [result, foldToScan(fold)(result)]
@@ -57,7 +57,7 @@ const foldToScan = fold => prior => i => {
57
57
 
58
58
  /**
59
59
  * @template T
60
- * @typedef {FoldT<T, T>} Reduce
60
+ * @typedef {Fold<T, T>} Reduce
61
61
  */
62
62
 
63
63
  /** @type {<T>(fold: Reduce<T>) => Scan<T, T>} */
@@ -250,10 +250,10 @@ const scan = op => apply(scanStep(op))
250
250
  /** @type {<I, S, O>(op: operator.StateScan<I, S, O>) => (init: S) => (input: List<I>) => Thunk<O>} */
251
251
  const stateScan = op => init => scan(stateScanToScan(op)(init))
252
252
 
253
- /** @type {<I,O>(op: operator.FoldT<I, O>) => (init: O) => (input: List<I>) => Thunk<O>} */
253
+ /** @type {<I,O>(op: operator.Fold<I, O>) => (init: O) => (input: List<I>) => Thunk<O>} */
254
254
  const foldScan = op => init => scan(foldToScan(op)(init))
255
255
 
256
- /** @type {<I,O>(op: operator.FoldT<I, O>) => (init: O) => (input: List<I>) => O} */
256
+ /** @type {<I,O>(op: operator.Fold<I, O>) => (init: O) => (input: List<I>) => O} */
257
257
  const fold = op => init => input => last(init)(foldScan(op)(init)(input))
258
258
 
259
259
  /** @type {<T>(op: operator.Reduce<T>) => <D>(def: D) => (input: List<T>) => D|T} */