functionalscript 0.0.233 → 0.0.234

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,71 @@
1
+ const json = require('../../../json')
2
+ const { isObject } = json
3
+ const seq = require('../../../types/sequence')
4
+ const { split } = require('../../path')
5
+ const map = require('../../../types/map')
6
+
7
+ /** @typedef {(directoryName: string) => undefined|string|Directory} Directory */
8
+
9
+ const empty = () => undefined
10
+
11
+ /**
12
+ * @typedef {{
13
+ * readonly get: (dir: string) => Item|undefined
14
+ * readonly set: (dir: string) => (item: Item) => Map
15
+ * }} Map
16
+ */
17
+
18
+ /** @typedef {Map|string} Item */
19
+
20
+ /** @typedef {readonly[string, Map]} Pair */
21
+
22
+ /** @type {(prior: seq.Sequence<Pair>) => (dir: string) => seq.Sequence<Pair>} */
23
+ const get = prior => dir => {
24
+ const result = seq.next(prior)
25
+ if (result === undefined) { throw 'panic' }
26
+ const { first: [,m] } = result
27
+ const child = m.get(dir)
28
+ const childMap = child === undefined || typeof child === 'string' ? map.empty : child
29
+ /** @type {Pair} */
30
+ const pair = [dir, childMap]
31
+ return seq.sequence(pair)(prior)
32
+ }
33
+
34
+ /** @typedef {readonly[string, Item]} Result */
35
+
36
+ /** @type {(a: Result) => (b: Pair) => Result} */
37
+ const set = ([aDir, item]) => ([bDir, bMap]) => [bDir, bMap.set(aDir)(item)]
38
+
39
+ /** @type {(prior: Map) => (entry: json.Entry) => Map} */
40
+ const addDirectory = prior => ([directory, id]) => {
41
+ if (typeof id !== 'string') { return prior }
42
+ const path = split(directory)
43
+ const rev = seq.reduce(get)([['', prior]])(path)
44
+ const result = seq.next(rev)
45
+ if (result === undefined) { throw 'panic' }
46
+ const { first: [dir], tail } = result
47
+ const [, m] = seq.reduce(set)([dir, id])(tail)
48
+ if (typeof m === 'string') { return prior }
49
+ return m
50
+ }
51
+
52
+ /** @type {(m: Map) => Directory} */
53
+ const func = m => dir => {
54
+ const r = m.get(dir)
55
+ if (typeof r !== 'object') { return r }
56
+ return func(r)
57
+ }
58
+
59
+ /** @type {(packageJson: json.Unknown) => Directory} */
60
+ const getDirectory = packageJson => {
61
+ if (!isObject(packageJson)) { return empty }
62
+ const dep = packageJson['dependencies']
63
+ if (dep === undefined || !isObject(dep)) { return empty }
64
+ const result = seq.reduce(addDirectory)(map.empty)(Object.entries(dep))
65
+ return func(result)
66
+ }
67
+
68
+ module.exports = {
69
+ /** @readonly */
70
+ getDirectory,
71
+ }
@@ -0,0 +1,33 @@
1
+ const _ = require('.')
2
+
3
+ {
4
+ const packageJson = { dependencies: {} }
5
+ const dir = _.getDirectory(packageJson)
6
+ const r = dir('x')
7
+ if (r !== undefined) { throw r }
8
+ }
9
+
10
+ {
11
+ const packageJson = { dependencies: { 'x': 'e' } }
12
+ const dir = _.getDirectory(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 = _.getDirectory(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
+ }
@@ -1,72 +1,37 @@
1
- const { todo } = require('../../dev')
2
1
  const json = require('../../json')
3
- const { isObject } = json
4
2
  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
3
 
12
4
  /**
13
5
  * @typedef {{
14
- * readonly get: (dir: string) => Item|undefined
15
- * readonly set: (dir: string) => (item: Item) => Map
16
- * }} Map
6
+ * readonly version: string
7
+ * readonly dependencies?: Dependencies
8
+ * }} Package
17
9
  */
18
10
 
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)
11
+ /** @type {(j: json.Unknown) => j is Package} */
12
+ const isPackage = j => {
13
+ if (!json.isObject(j)) { return false }
14
+ if (typeof j.version !== 'string') { return false }
15
+ if (j.dependencies !== undefined && !isDependencies(j.dependencies)) { return false }
16
+ return true
33
17
  }
34
18
 
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
- }
19
+ /**
20
+ * @typedef {{
21
+ * readonly [k in string]: string
22
+ * }} Dependencies
23
+ */
52
24
 
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
- }
25
+ /** @type {(entry: json.Entry) => boolean} */
26
+ const isDependency = ([,v]) => typeof v === 'string'
59
27
 
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)
28
+ /** @type {(j: json.Unknown) => j is Dependencies} */
29
+ const isDependencies = j => {
30
+ if (!json.isObject(j)) { return false }
31
+ return seq.every(seq.map(isDependency)(Object.entries(j)))
67
32
  }
68
33
 
69
34
  module.exports = {
70
35
  /** @readonly */
71
- dependencies,
36
+ isPackage,
72
37
  }
@@ -1,33 +1,14 @@
1
1
  const _ = require('.')
2
2
 
3
3
  {
4
- const packageJson = { dependencies: {} }
5
- const dir = _.dependencies(packageJson)
6
- const r = dir('x')
7
- if (r !== undefined) { throw r }
4
+ if (_.isPackage(null)) { throw 'error' }
5
+ if (_.isPackage({})) { throw 'error' }
6
+ if (_.isPackage({version:12})) { throw 'error' }
7
+ if (!_.isPackage({version:"12"})) { throw 'error' }
8
+ if (_.isPackage({version:"",dependencies:[]})) { throw 'error' }
9
+ if (!_.isPackage({ version: "", dependencies: {} })) { throw 'error' }
10
+ if (_.isPackage({ version: "", dependencies: { x: 12} })) { throw 'error' }
11
+ if (!_.isPackage({ version: "", dependencies: { x: "12" } })) { throw 'error' }
8
12
  }
9
13
 
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
- }
14
+ module.exports = {}
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "functionalscript",
3
- "version": "0.0.233",
3
+ "version": "0.0.234",
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/dependencies/test')
8
9
  require('./commonjs/package/test')
9
10
 
10
11
  /** @type {() => never} */
package/version.js CHANGED
@@ -1,6 +1,7 @@
1
1
  const cp = require('child_process')
2
2
  const fs = require('fs')
3
3
  const json = require('./json')
4
+ const { isPackage } = require('./commonjs/package')
4
5
 
5
6
  const b =cp.execSync('git log --oneline')
6
7
 
@@ -10,8 +11,10 @@ const v = `0.0.${r}`
10
11
 
11
12
  console.log(`version: ${v}`)
12
13
 
13
- let package_json = json.parse(fs.readFileSync('package.json').toString())
14
+ const package_json = json.parse(fs.readFileSync('package.json').toString())
14
15
 
15
- package_json = json.setProperty(v)(['version'])(package_json)
16
+ if (!isPackage(package_json)) { throw 'error' }
16
17
 
17
- fs.writeFileSync('package.json', JSON.stringify(package_json, null, 2))
18
+ const x = { ...package_json, version: v }
19
+
20
+ fs.writeFileSync('package.json', JSON.stringify(x, null, 2))