functionalscript 0.0.232 → 0.0.233

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.
@@ -0,0 +1,72 @@
1
+ const { todo } = require('../../dev')
2
+ const json = require('../../json')
3
+ const { isObject } = json
4
+ const seq = require('../../types/sequence')
5
+ const { split } = require('../path')
6
+ const map = require('../../types/map')
7
+
8
+ /** @typedef {(directoryName: string) => undefined|string|Directory} Directory */
9
+
10
+ const empty = () => undefined
11
+
12
+ /**
13
+ * @typedef {{
14
+ * readonly get: (dir: string) => Item|undefined
15
+ * readonly set: (dir: string) => (item: Item) => Map
16
+ * }} Map
17
+ */
18
+
19
+ /** @typedef {Map|string} Item */
20
+
21
+ /** @typedef {readonly[string, Map]} Pair */
22
+
23
+ /** @type {(prior: seq.Sequence<Pair>) => (dir: string) => seq.Sequence<Pair>} */
24
+ const get = prior => dir => {
25
+ const result = seq.next(prior)
26
+ if (result === undefined) { throw 'panic' }
27
+ const { first: [,m] } = result
28
+ const child = m.get(dir)
29
+ const childMap = child === undefined || typeof child === 'string' ? map.empty : child
30
+ /** @type {Pair} */
31
+ const pair = [dir, childMap]
32
+ return seq.sequence(pair)(prior)
33
+ }
34
+
35
+ /** @typedef {readonly[string, Item]} Result */
36
+
37
+ /** @type {(a: Result) => (b: Pair) => Result} */
38
+ const set = ([aDir, item]) => ([bDir, bMap]) => [bDir, bMap.set(aDir)(item)]
39
+
40
+ /** @type {(prior: Map) => (entry: json.Entry) => Map} */
41
+ const addDirectory = prior => ([directory, id]) => {
42
+ if (typeof id !== 'string') { return prior }
43
+ const path = split(directory)
44
+ const rev = seq.reduce(get)([['', prior]])(path)
45
+ const result = seq.next(rev)
46
+ if (result === undefined) { throw 'panic' }
47
+ const { first: [dir], tail } = result
48
+ const [, m] = seq.reduce(set)([dir, id])(tail)
49
+ if (typeof m === 'string') { return prior }
50
+ return m
51
+ }
52
+
53
+ /** @type {(m: Map) => Directory} */
54
+ const func = m => dir => {
55
+ const r = m.get(dir)
56
+ if (typeof r !== 'object') { return r }
57
+ return func(r)
58
+ }
59
+
60
+ /** @type {(packageJson: json.Unknown) => Directory} */
61
+ const dependencies = packageJson => {
62
+ if (!isObject(packageJson)) { return empty }
63
+ const dependencies = packageJson['dependencies']
64
+ if (dependencies === undefined || !isObject(dependencies)) { return empty }
65
+ const result = seq.reduce(addDirectory)(map.empty)(Object.entries(dependencies))
66
+ return func(result)
67
+ }
68
+
69
+ module.exports = {
70
+ /** @readonly */
71
+ dependencies,
72
+ }
@@ -0,0 +1,33 @@
1
+ const _ = require('.')
2
+
3
+ {
4
+ const packageJson = { dependencies: {} }
5
+ const dir = _.dependencies(packageJson)
6
+ const r = dir('x')
7
+ if (r !== undefined) { throw r }
8
+ }
9
+
10
+ {
11
+ const packageJson = { dependencies: { 'x': 'e' } }
12
+ const dir = _.dependencies(packageJson)
13
+ const r = dir('x')
14
+ if (r !== 'e') { throw r }
15
+ }
16
+
17
+ {
18
+ const packageJson = {
19
+ dependencies: {
20
+ 'x/a': 'xa',
21
+ 'x/b': 'xb'
22
+ }
23
+ }
24
+ const dir = _.dependencies(packageJson)
25
+ const x = dir('x')
26
+ if (typeof x !== 'function') { throw x }
27
+ const xa = x('a')
28
+ if (xa !== 'xa') { throw xa }
29
+ const xb = x('b')
30
+ if (xb !== 'xb') { throw xb }
31
+ const xc = x('c')
32
+ if (xc !== undefined) { throw xc }
33
+ }
@@ -12,7 +12,7 @@ const { compose } = require("../../types/function")
12
12
  * }} Path
13
13
  */
14
14
 
15
- /** @type {(path: string) => seq.Sequence<string>} */
15
+ /** @type {(path: string) => readonly string[]} */
16
16
  const split = path => path.split('/')
17
17
 
18
18
  /** @type {(s: undefined|seq.Sequence<string>) => (items: string) => undefined|seq.Sequence<string>} */
@@ -59,6 +59,8 @@ const parse = local => {
59
59
  }
60
60
 
61
61
  module.exports = {
62
+ /** @readonly */
63
+ split,
62
64
  /** @readonly */
63
65
  parse,
64
66
  }
@@ -1,34 +1,28 @@
1
- const { ok, error } = require('../../types/result')
1
+ const { tryCatch } = require('../result')
2
+ const { unwrap } = require('../../types/result')
2
3
  const run = require('../../commonjs/run')
3
4
 
4
5
  /** @type {(f: Function) => run.Module} */
5
- const runModule = f => req => info => {
6
+ const createModule = f => req => mutableInfo => {
6
7
  /** @type {(path: string) => unknown} */
7
8
  const mutableRequire = path => {
8
- const [result, newInfo] = req(info)(path)
9
- info = newInfo
10
- if (result[0] === 'error') { throw result[1] }
11
- return result[1]
9
+ const [result, info] = req(mutableInfo)(path)
10
+ mutableInfo = info
11
+ return unwrap(result)
12
12
  }
13
- const mutableModule = { exports: {} }
14
- try {
13
+ const result = tryCatch(() => {
14
+ const mutableModule = { exports: {} }
15
15
  f(mutableModule, mutableRequire)
16
- } catch (e) {
17
- return [error(e), info]
18
- }
19
- return [ok(mutableModule.exports), info]
16
+ return mutableModule.exports
17
+ })
18
+ return [result, mutableInfo]
20
19
  }
21
20
 
22
21
  /** @type {run.Compile} */
23
- const compile = source => {
24
- try {
25
- return ok(runModule(Function('module', 'require', `"use strict";${source}`)))
26
- } catch (e) {
27
- return error(e)
28
- }
29
- }
22
+ const compile = source =>
23
+ tryCatch(() => createModule(Function('module', 'require', `"use strict";${source}`)))
30
24
 
31
25
  module.exports = {
32
26
  /** @readonly */
33
27
  compile,
34
- }
28
+ }
@@ -0,0 +1,15 @@
1
+ const result = require('../../types/result')
2
+
3
+ /** @type {<T>(f: () => T) => result.Result<T, unknown>} */
4
+ const tryCatch = f => {
5
+ try {
6
+ return result.ok(f())
7
+ } catch (e) {
8
+ return result.error(e)
9
+ }
10
+ }
11
+
12
+ module.exports = {
13
+ /** @readonly */
14
+ tryCatch,
15
+ }
package/json/index.js CHANGED
@@ -1,6 +1,7 @@
1
1
  const seq = require('../types/sequence')
2
2
  const object = require('../types/object')
3
3
  const array = require('../types/array')
4
+ const { todo } = require('../dev')
4
5
 
5
6
  /**
6
7
  * @typedef {{
@@ -104,8 +105,6 @@ const serialize = sort => {
104
105
  }
105
106
 
106
107
  /**
107
- * A version of `JSON.stringify` with an alphabeticly ordered `keys`.
108
- *
109
108
  * The standard `JSON.stringify` rules determined by
110
109
  * https://262.ecma-international.org/6.0/#sec-ordinary-object-internal-methods-and-internal-slots-ownpropertykeys
111
110
  *
@@ -119,6 +118,9 @@ const stringify = sort => value => {
119
118
  /** @type {(value: string) => Unknown} */
120
119
  const parse = value => JSON.parse(value)
121
120
 
121
+ /** @type {(value: Unknown) => value is Object} */
122
+ const isObject = value => typeof value === 'object' && value !== null && !(value instanceof Array)
123
+
122
124
  module.exports = {
123
125
  /** @readonly */
124
126
  setProperty,
@@ -128,4 +130,6 @@ module.exports = {
128
130
  serialize,
129
131
  /** @readonly */
130
132
  parse,
131
- }
133
+ /** @readonly */
134
+ isObject,
135
+ }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "functionalscript",
3
- "version": "0.0.232",
3
+ "version": "0.0.233",
4
4
  "description": "FunctionalScript is a functional subset of JavaScript",
5
5
  "main": "index.js",
6
6
  "scripts": {
package/test.js CHANGED
@@ -5,6 +5,7 @@ require('./types/btree/test')
5
5
  require('./json/test')
6
6
  require('./types/object/test')
7
7
  require('./io/commonjs/test')
8
+ require('./commonjs/package/test')
8
9
 
9
10
  /** @type {() => never} */
10
11
  const assert = () => { throw 'assert' }
@@ -20,9 +20,17 @@ const ok = value => ['ok', value]
20
20
  /** @type {<E>(e: E) => Error<E>} */
21
21
  const error = e => ['error', e]
22
22
 
23
+ /** @type {<T, E>(r: Result<T, E>) => T} */
24
+ const unwrap = result => {
25
+ if (result[0] === 'error') { throw result[1] }
26
+ return result[1]
27
+ }
28
+
23
29
  module.exports = {
24
30
  /** @readonly */
25
31
  ok,
26
32
  /** @readonly */
27
33
  error,
34
+ /** @readonly */
35
+ unwrap,
28
36
  }