functionalscript 0.0.0 → 0.0.126

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.
@@ -1,98 +1,98 @@
1
- const lib = require('../lib')
2
- const iter = require('../lib/iterable')
3
-
4
- /** @typedef {(_: string) => string|undefined} ReadFile */
5
-
6
- /**
7
- * @typedef {{
8
- * id: string[]
9
- * dependencies: Dependencies
10
- * file: ReadFile
11
- * }} Package
12
- */
13
-
14
- /**
15
- * @typedef {{
16
- * pack: Package,
17
- * local: string[],
18
- * }} Location
19
- */
20
-
21
- /**
22
- * @typedef {{
23
- * fileName: string
24
- * location: Location
25
- * source: string
26
- * }} Module
27
- */
28
-
29
- /** @typedef {(_: string) => undefined|Package|Dependencies} Dependencies */
30
-
31
- /** @type {lib.Reduce<string, undefined|string[]>} */
32
- const pathNormReduce = {
33
- merge: path => item =>
34
- path === undefined ?
35
- undefined :
36
- ['', '.'].includes(item) ?
37
- path :
38
- item === '..' ?
39
- lib.head(path) :
40
- [...path, item],
41
- init: []
42
- }
43
-
44
- /** @type {(_: string[]) => boolean} */
45
- const isRelative = localId => ['.', '..'].includes(localId[0])
46
-
47
- const pathNorm = iter.reduce(pathNormReduce)
48
-
49
- /** @type {(_: Package) => (_: string[]) => Module|undefined} */
50
- const internal = pack => {
51
- /** @type {(_: string[]) => (_: string) => Module|undefined} */
52
- const readFile = local => fileName => {
53
- const source = pack.file([...local, fileName].join('/'))
54
- return source === undefined ? undefined : { fileName, location: { pack, local }, source}
55
- }
56
- return path => {
57
- /** @type {(_: string[]) => Module|undefined} */
58
- const read = local => {
59
- /** @type {(_: [string[], string]) => Module|undefined} */
60
- const tryFiles = ([head, last]) => {
61
- /** @type {(_: string) => Module|undefined} */
62
- const one = ext => readFile(head)(last + ext)
63
- return ['.', '..', '', undefined].includes(lib.last(path)) ? undefined : one('') ?? one('.js')
64
- }
65
- return lib.optionMap(tryFiles)(lib.splitLast(local)) ?? readFile(local)('index.js')
66
- }
67
- return lib.optionMap(read)(pathNorm(path))
68
- }
69
- }
70
-
71
- /** @type {(_: Package|Dependencies|undefined) => (_: string[]) => Module|undefined} */
72
- const externalOrInternal = p =>
73
- p === undefined ? () => undefined : (typeof p === 'function' ? external(p) : internal(p))
74
-
75
- /** @type {(_: Dependencies) => (_: string[]) => Module|undefined} */
76
- const external = packages => {
77
- /** @type {(_: [string, string[]]) => Module|undefined} */
78
- const defined = ([first, tail]) => externalOrInternal(packages(first))(tail)
79
- /** @type {(_: string[]) => [string, string[]]|undefined} */
80
- const splitFirst = lib.splitFirst
81
- return lib.pipe(splitFirst)(lib.optionMap(defined))
82
- }
83
-
84
- /** @type {(_: Location) => (_: string) => Module|undefined} */
85
- const getModule = ({pack, local}) => path => {
86
- const pathArray = path.split('/')
87
- return isRelative(pathArray) ? internal(pack)([...local, ...pathArray]) : external(pack.dependencies)(pathArray)
88
- }
89
-
90
- /** @type {(_: Module) => string} */
91
- const moduleId = module => [...module.location.pack.id, ...module.location.local, module.fileName].join('/')
92
-
93
- module.exports = {
94
- isRelative,
95
- pathNorm,
96
- getModule,
97
- moduleId,
1
+ const lib = require('../lib')
2
+ const iter = require('../lib/iterable')
3
+
4
+ /** @typedef {(_: string) => string|undefined} ReadFile */
5
+
6
+ /**
7
+ * @typedef {{
8
+ * id: string[]
9
+ * dependencies: Dependencies
10
+ * file: ReadFile
11
+ * }} Package
12
+ */
13
+
14
+ /**
15
+ * @typedef {{
16
+ * pack: Package,
17
+ * local: string[],
18
+ * }} Location
19
+ */
20
+
21
+ /**
22
+ * @typedef {{
23
+ * fileName: string
24
+ * location: Location
25
+ * source: string
26
+ * }} Module
27
+ */
28
+
29
+ /** @typedef {(_: string) => undefined|Package|Dependencies} Dependencies */
30
+
31
+ /** @type {lib.Reduce<string, undefined|string[]>} */
32
+ const pathNormReduce = {
33
+ merge: path => item =>
34
+ path === undefined ?
35
+ undefined :
36
+ ['', '.'].includes(item) ?
37
+ path :
38
+ item === '..' ?
39
+ lib.head(path) :
40
+ [...path, item],
41
+ init: []
42
+ }
43
+
44
+ /** @type {(_: string[]) => boolean} */
45
+ const isRelative = localId => ['.', '..'].includes(localId[0])
46
+
47
+ const pathNorm = iter.reduce(pathNormReduce)
48
+
49
+ /** @type {(_: Package) => (_: string[]) => Module|undefined} */
50
+ const internal = pack => {
51
+ /** @type {(_: string[]) => (_: string) => Module|undefined} */
52
+ const readFile = local => fileName => {
53
+ const source = pack.file([...local, fileName].join('/'))
54
+ return source === undefined ? undefined : { fileName, location: { pack, local }, source}
55
+ }
56
+ return path => {
57
+ /** @type {(_: string[]) => Module|undefined} */
58
+ const read = local => {
59
+ /** @type {(_: [string[], string]) => Module|undefined} */
60
+ const tryFiles = ([head, last]) => {
61
+ /** @type {(_: string) => Module|undefined} */
62
+ const one = ext => readFile(head)(last + ext)
63
+ return ['.', '..', '', undefined].includes(lib.last(path)) ? undefined : one('') ?? one('.js')
64
+ }
65
+ return lib.optionMap(tryFiles)(lib.splitLast(local)) ?? readFile(local)('index.js')
66
+ }
67
+ return lib.optionMap(read)(pathNorm(path))
68
+ }
69
+ }
70
+
71
+ /** @type {(_: Package|Dependencies|undefined) => (_: string[]) => Module|undefined} */
72
+ const externalOrInternal = p =>
73
+ p === undefined ? () => undefined : (typeof p === 'function' ? external(p) : internal(p))
74
+
75
+ /** @type {(_: Dependencies) => (_: string[]) => Module|undefined} */
76
+ const external = packages => {
77
+ /** @type {(_: [string, string[]]) => Module|undefined} */
78
+ const defined = ([first, tail]) => externalOrInternal(packages(first))(tail)
79
+ /** @type {(_: string[]) => [string, string[]]|undefined} */
80
+ const splitFirst = lib.splitFirst
81
+ return lib.pipe(splitFirst)(lib.optionMap(defined))
82
+ }
83
+
84
+ /** @type {(_: Location) => (_: string) => Module|undefined} */
85
+ const getModule = ({pack, local}) => path => {
86
+ const pathArray = path.split('/')
87
+ return isRelative(pathArray) ? internal(pack)([...local, ...pathArray]) : external(pack.dependencies)(pathArray)
88
+ }
89
+
90
+ /** @type {(_: Module) => string} */
91
+ const moduleId = module => [...module.location.pack.id, ...module.location.local, module.fileName].join('/')
92
+
93
+ module.exports = {
94
+ isRelative,
95
+ pathNorm,
96
+ getModule,
97
+ moduleId,
98
98
  }
@@ -1,18 +1,18 @@
1
- const m = require('..')
2
-
3
- /** @type {(_: m.ReadFile) => m.Location} */
4
- module.exports = readFile => {
5
- /** @type {(_: string[]) => (_: string) => m.Package|m.Dependencies|undefined} */
6
- const packages = path => name => {
7
- const newPath = [...path, name]
8
- // we only need to check if 'package.json' exist
9
- return (readFile([...newPath, 'package.json'].join('/')) === undefined ? packages : pack)(newPath)
10
- }
11
- /** @type {(_: string[]) => m.Package} */
12
- const pack = path => ({
13
- id: path,
14
- dependencies: packages(['node_modules']),
15
- file: filePath => readFile([...path, ...filePath.split('/')].join('/'))
16
- })
17
- return { pack: pack([]), local: [] }
18
- }
1
+ const m = require('..')
2
+
3
+ /** @type {(_: m.ReadFile) => m.Location} */
4
+ module.exports = readFile => {
5
+ /** @type {(_: string[]) => (_: string) => m.Package|m.Dependencies|undefined} */
6
+ const packages = path => name => {
7
+ const newPath = [...path, name]
8
+ // we only need to check if 'package.json' exist
9
+ return (readFile([...newPath, 'package.json'].join('/')) === undefined ? packages : pack)(newPath)
10
+ }
11
+ /** @type {(_: string[]) => m.Package} */
12
+ const pack = path => ({
13
+ id: path,
14
+ dependencies: packages(['node_modules']),
15
+ file: filePath => readFile([...path, ...filePath.split('/')].join('/'))
16
+ })
17
+ return { pack: pack([]), local: [] }
18
+ }
@@ -1,76 +1,76 @@
1
- const lib = require('../../lib')
2
- const mm = require('..')
3
- const i = require('.')
4
-
5
- /** @type {{ [_ in string]: string}} */
6
- const files = {
7
- 'index.js': './index.js',
8
- 'a/index.js': './a/index.js',
9
- 'a/b/x.js': './a/b/x.js',
10
- 'c/t.js': './c/t.js',
11
- 'node_modules/@functionalscript/functionalscript/package.json': '',
12
- 'node_modules/@functionalscript/functionalscript/index.js': '@functionalscript/functionalscript ./index.js',
13
- 'node_modules/my/package.json': '',
14
- 'node_modules/my/src/x.js': 'my ./src/x.js',
15
- 'node_modules/my/b/index.js': 'my ./b/index.js',
16
- }
17
-
18
- /** @type {(_: string) => string|undefined} */
19
- const readFile = path => files[path]
20
-
21
- const root = i(readFile)
22
-
23
- {
24
- const index = mm.getModule(root)('.')
25
- if (index === undefined) { throw 'no module' }
26
- if (index.fileName !== 'index.js') { throw 'fileName' }
27
- if (index.source !== './index.js') { throw 'source' }
28
- if (index.location.local.join('/') !== '') { throw 'location' }
29
- if (index.location.pack.id.join('/') !== '') { throw 'pack.id' }
30
- if (mm.moduleId(index) !== 'index.js') { throw 'moduleId' }
31
- const a = mm.getModule(index.location)('./a')
32
- if (a === undefined) { throw 'no module' }
33
- if (a.fileName !== 'index.js') { throw 'fileName' }
34
- if (a.source !== './a/index.js') { throw 'source' }
35
- if (a.location.local.join('/') !== 'a') { throw 'location' }
36
- if (a.location.pack.id.join('/') !== '') { throw 'pack.id' }
37
- if (mm.moduleId(a) !== 'a/index.js') { throw 'moduleId' }
38
- const abx = mm.getModule(a.location)('./b/x')
39
- if (abx === undefined) { throw 'no module' }
40
- if (abx.fileName !== 'x.js') { throw 'fileName' }
41
- if (abx.source !== './a/b/x.js') { throw 'source' }
42
- if (abx.location.local.join('/') !== 'a/b') { throw 'location' }
43
- if (abx.location.pack.id.join('/') !== '') { throw 'pack.id' }
44
- if (mm.moduleId(abx) !== 'a/b/x.js') { throw 'moduleId' }
45
- const ct = mm.getModule(abx.location)('../../c/t.js')
46
- if (ct === undefined) { throw 'no module' }
47
- if (ct.fileName !== 't.js') { throw 'fileName' }
48
- if (ct.source !== './c/t.js') { throw 'source' }
49
- if (ct.location.local.join('/') !== 'c') { throw 'location' }
50
- if (ct.location.pack.id.join('/') !== '') { throw 'pack.id' }
51
- if (mm.getModule(ct.location)('./d') !== undefined) { throw 'no module' }
52
- if (mm.getModule(ct.location)('@functionalscript') !== undefined) { throw 'no module' }
53
- if (mm.moduleId(ct) !== 'c/t.js') { throw 'moduleId' }
54
- const fs = mm.getModule(ct.location)('@functionalscript/functionalscript')
55
- if (fs === undefined) { throw 'no module' }
56
- if (fs.source !== '@functionalscript/functionalscript ./index.js') { throw 'source' }
57
- if (fs.fileName !== 'index.js') { throw 'fileName' }
58
- if (fs.location.local.join('/') !== '') { throw 'location' }
59
- if (fs.location.pack.id.join('/') !== 'node_modules/@functionalscript/functionalscript') { throw 'pack.id' }
60
- if (mm.getModule(fs.location)('my/src/x/') !== undefined) { throw 'no module '}
61
- if (mm.moduleId(fs) !== 'node_modules/@functionalscript/functionalscript/index.js') { throw 'moduleId' }
62
- const mySrcX = mm.getModule(fs.location)('my/src/x')
63
- if (mySrcX === undefined) { throw 'no module' }
64
- if (mySrcX.fileName !== 'x.js') { throw 'fileName' }
65
- if (mySrcX.source !== 'my ./src/x.js') { throw 'source' }
66
- if (mySrcX.location.local.join('/') !== 'src') { throw 'location' }
67
- if (mySrcX.location.pack.id.join('/') !== 'node_modules/my') { throw 'pack.id' }
68
- if (mm.moduleId(mySrcX) !== 'node_modules/my/src/x.js') { throw 'moduleId' }
69
- const myB = mm.getModule(mySrcX.location)('../b/')
70
- if (myB === undefined) { throw 'no module' }
71
- if (myB.fileName !== 'index.js') { throw 'fileName' }
72
- if (myB.source !== 'my ./b/index.js') { throw 'source' }
73
- if (myB.location.local.join('/') !== 'b') { throw 'location' }
74
- if (myB.location.pack.id.join('/') !== 'node_modules/my') { throw 'pack.id' }
75
- if (mm.moduleId(myB) !== 'node_modules/my/b/index.js') { throw 'moduleId' }
1
+ const lib = require('../../lib')
2
+ const mm = require('..')
3
+ const i = require('.')
4
+
5
+ /** @type {{ [_ in string]: string}} */
6
+ const files = {
7
+ 'index.js': './index.js',
8
+ 'a/index.js': './a/index.js',
9
+ 'a/b/x.js': './a/b/x.js',
10
+ 'c/t.js': './c/t.js',
11
+ 'node_modules/@functionalscript/functionalscript/package.json': '',
12
+ 'node_modules/@functionalscript/functionalscript/index.js': '@functionalscript/functionalscript ./index.js',
13
+ 'node_modules/my/package.json': '',
14
+ 'node_modules/my/src/x.js': 'my ./src/x.js',
15
+ 'node_modules/my/b/index.js': 'my ./b/index.js',
16
+ }
17
+
18
+ /** @type {(_: string) => string|undefined} */
19
+ const readFile = path => files[path]
20
+
21
+ const root = i(readFile)
22
+
23
+ {
24
+ const index = mm.getModule(root)('.')
25
+ if (index === undefined) { throw 'no module' }
26
+ if (index.fileName !== 'index.js') { throw 'fileName' }
27
+ if (index.source !== './index.js') { throw 'source' }
28
+ if (index.location.local.join('/') !== '') { throw 'location' }
29
+ if (index.location.pack.id.join('/') !== '') { throw 'pack.id' }
30
+ if (mm.moduleId(index) !== 'index.js') { throw 'moduleId' }
31
+ const a = mm.getModule(index.location)('./a')
32
+ if (a === undefined) { throw 'no module' }
33
+ if (a.fileName !== 'index.js') { throw 'fileName' }
34
+ if (a.source !== './a/index.js') { throw 'source' }
35
+ if (a.location.local.join('/') !== 'a') { throw 'location' }
36
+ if (a.location.pack.id.join('/') !== '') { throw 'pack.id' }
37
+ if (mm.moduleId(a) !== 'a/index.js') { throw 'moduleId' }
38
+ const abx = mm.getModule(a.location)('./b/x')
39
+ if (abx === undefined) { throw 'no module' }
40
+ if (abx.fileName !== 'x.js') { throw 'fileName' }
41
+ if (abx.source !== './a/b/x.js') { throw 'source' }
42
+ if (abx.location.local.join('/') !== 'a/b') { throw 'location' }
43
+ if (abx.location.pack.id.join('/') !== '') { throw 'pack.id' }
44
+ if (mm.moduleId(abx) !== 'a/b/x.js') { throw 'moduleId' }
45
+ const ct = mm.getModule(abx.location)('../../c/t.js')
46
+ if (ct === undefined) { throw 'no module' }
47
+ if (ct.fileName !== 't.js') { throw 'fileName' }
48
+ if (ct.source !== './c/t.js') { throw 'source' }
49
+ if (ct.location.local.join('/') !== 'c') { throw 'location' }
50
+ if (ct.location.pack.id.join('/') !== '') { throw 'pack.id' }
51
+ if (mm.getModule(ct.location)('./d') !== undefined) { throw 'no module' }
52
+ if (mm.getModule(ct.location)('@functionalscript') !== undefined) { throw 'no module' }
53
+ if (mm.moduleId(ct) !== 'c/t.js') { throw 'moduleId' }
54
+ const fs = mm.getModule(ct.location)('@functionalscript/functionalscript')
55
+ if (fs === undefined) { throw 'no module' }
56
+ if (fs.source !== '@functionalscript/functionalscript ./index.js') { throw 'source' }
57
+ if (fs.fileName !== 'index.js') { throw 'fileName' }
58
+ if (fs.location.local.join('/') !== '') { throw 'location' }
59
+ if (fs.location.pack.id.join('/') !== 'node_modules/@functionalscript/functionalscript') { throw 'pack.id' }
60
+ if (mm.getModule(fs.location)('my/src/x/') !== undefined) { throw 'no module '}
61
+ if (mm.moduleId(fs) !== 'node_modules/@functionalscript/functionalscript/index.js') { throw 'moduleId' }
62
+ const mySrcX = mm.getModule(fs.location)('my/src/x')
63
+ if (mySrcX === undefined) { throw 'no module' }
64
+ if (mySrcX.fileName !== 'x.js') { throw 'fileName' }
65
+ if (mySrcX.source !== 'my ./src/x.js') { throw 'source' }
66
+ if (mySrcX.location.local.join('/') !== 'src') { throw 'location' }
67
+ if (mySrcX.location.pack.id.join('/') !== 'node_modules/my') { throw 'pack.id' }
68
+ if (mm.moduleId(mySrcX) !== 'node_modules/my/src/x.js') { throw 'moduleId' }
69
+ const myB = mm.getModule(mySrcX.location)('../b/')
70
+ if (myB === undefined) { throw 'no module' }
71
+ if (myB.fileName !== 'index.js') { throw 'fileName' }
72
+ if (myB.source !== 'my ./b/index.js') { throw 'source' }
73
+ if (myB.location.local.join('/') !== 'b') { throw 'location' }
74
+ if (myB.location.pack.id.join('/') !== 'node_modules/my') { throw 'pack.id' }
75
+ if (mm.moduleId(myB) !== 'node_modules/my/b/index.js') { throw 'moduleId' }
76
76
  }