@junoscript/juno 0.0.0-stage → 0.1.0
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/Modules/async.ts +81 -0
- package/Modules/juno-node-fs.ts +105 -0
- package/Modules/juno-test.jn +156 -0
- package/dist/CLI/build-juno.d.ts +5 -0
- package/dist/CLI/build-juno.js +58 -0
- package/dist/CLI/generate-juno-types.d.ts +1 -0
- package/dist/CLI/generate-juno-types.js +51 -0
- package/dist/CLI/junode.d.ts +2 -0
- package/dist/CLI/junode.js +32 -0
- package/dist/Lexer/lexer.d.ts +42 -0
- package/dist/Lexer/lexer.js +300 -0
- package/dist/Lexer/parser.d.ts +395 -0
- package/dist/Lexer/parser.js +1881 -0
- package/dist/Modules/async.d.ts +33 -0
- package/dist/Modules/async.js +76 -0
- package/dist/Modules/index.d.ts +195 -0
- package/dist/Modules/index.js +31 -0
- package/dist/Modules/juno-node-fs.d.ts +52 -0
- package/dist/Modules/juno-node-fs.js +129 -0
- package/dist/Modules/juno-node.d.ts +74 -0
- package/dist/Modules/juno-node.js +56 -0
- package/dist/Modules/juno-test.d.ts +141 -0
- package/dist/Modules/juno-test.js +134 -0
- package/dist/Modules/juno-types.d.ts +35 -0
- package/dist/Modules/juno-types.js +2 -0
- package/dist/Modules/juno.d.ts +21 -0
- package/dist/Modules/juno.js +76 -0
- package/dist/Modules/types.d.ts +56 -0
- package/dist/Modules/types.js +87 -0
- package/dist/Runtime/interpreter.d.ts +64 -0
- package/dist/Runtime/interpreter.js +2533 -0
- package/dist/Runtime/resolve-import.d.ts +2 -0
- package/dist/Runtime/resolve-import.js +30 -0
- package/dist/vscode-junoscript/src/language.d.ts +86 -0
- package/dist/vscode-junoscript/src/language.js +4206 -0
- package/package.json +19 -4
- package/types/array.jn +158 -0
- package/types/boolean.jn +18 -0
- package/types/error.jn +28 -0
- package/types/error.ts +9 -0
- package/types/index.jn +11 -0
- package/types/iterable.jn +211 -0
- package/types/map.jn +44 -0
- package/types/map.ts +132 -0
- package/types/number.ts +52 -0
- package/types/object.jn +94 -0
- package/types/object.ts +49 -0
- package/types/queue.jn +46 -0
- package/types/random.jn +25 -0
- package/types/random.ts +1 -0
- package/types/range.jn +27 -0
- package/types/set.jn +139 -0
- package/types/set.ts +209 -0
- package/types/string.jn +33 -0
- package/types/string.ts +1 -0
- package/types/tuple.jn +11 -0
- package/README.md +0 -3
package/package.json
CHANGED
|
@@ -1,6 +1,21 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@junoscript/juno",
|
|
3
|
-
"version": "0.
|
|
4
|
-
"
|
|
5
|
-
"
|
|
6
|
-
|
|
3
|
+
"version": "0.1.0",
|
|
4
|
+
"description": "JunoScript runtime, standard library and junode CLI",
|
|
5
|
+
"main": "./dist/Runtime/interpreter.js",
|
|
6
|
+
"types": "./dist/Runtime/interpreter.d.ts",
|
|
7
|
+
"exports": {
|
|
8
|
+
".": "./dist/Runtime/interpreter.js",
|
|
9
|
+
"./modules": "./dist/Modules/index.js"
|
|
10
|
+
},
|
|
11
|
+
"bin": { "junode": "./dist/CLI/junode.js" },
|
|
12
|
+
"files": ["dist", "types", "Modules"],
|
|
13
|
+
"engines": { "node": ">=22.18.0" },
|
|
14
|
+
"publishConfig": { "access": "public" },
|
|
15
|
+
"scripts": { "prepack": "node ../../scripts/stage-npm.cjs juno" },
|
|
16
|
+
"dependencies": {
|
|
17
|
+
"@jest/fake-timers": "^30.5.2",
|
|
18
|
+
"expect": "^30.5.2",
|
|
19
|
+
"jest-mock": "^30.5.2"
|
|
20
|
+
}
|
|
21
|
+
}
|
package/types/array.jn
ADDED
|
@@ -0,0 +1,158 @@
|
|
|
1
|
+
import { Iterable } from './iterable'
|
|
2
|
+
import { Range } from './range'
|
|
3
|
+
|
|
4
|
+
export constructor Array<T> : Iterable<T> & T[] {
|
|
5
|
+
fun (length: number, options: {
|
|
6
|
+
fill: (T?, number?) -> T
|
|
7
|
+
}?) =
|
|
8
|
+
let _length = 0
|
|
9
|
+
let result = {
|
|
10
|
+
get length(): number =
|
|
11
|
+
return _length
|
|
12
|
+
|
|
13
|
+
[index : number] : T
|
|
14
|
+
|
|
15
|
+
get indices(): Range =
|
|
16
|
+
return Range(this.length)
|
|
17
|
+
|
|
18
|
+
fun copy(): T[] =
|
|
19
|
+
let array: T[] = []
|
|
20
|
+
array.push(...this)
|
|
21
|
+
return array
|
|
22
|
+
|
|
23
|
+
fun toArray(): T[] =
|
|
24
|
+
return this.copy()
|
|
25
|
+
|
|
26
|
+
fun enumerate(): [number, T][] =
|
|
27
|
+
let entries: (number | T)[][] = []
|
|
28
|
+
for const index of this.indices
|
|
29
|
+
entries.push([index, this[index]])
|
|
30
|
+
return entries
|
|
31
|
+
|
|
32
|
+
fun push(...items: T[]): number =
|
|
33
|
+
const previousLength = _length
|
|
34
|
+
_length += items.length
|
|
35
|
+
for const index of items.indices
|
|
36
|
+
this[previousLength + index] = items[index]
|
|
37
|
+
return _length
|
|
38
|
+
|
|
39
|
+
fun pop(): T? =
|
|
40
|
+
if _length == 0
|
|
41
|
+
return null
|
|
42
|
+
const value = this[_length - 1]
|
|
43
|
+
_length--
|
|
44
|
+
return value
|
|
45
|
+
|
|
46
|
+
fun shift(): T? =
|
|
47
|
+
if _length == 0
|
|
48
|
+
return null
|
|
49
|
+
const value = this[0]
|
|
50
|
+
for const index of this.indices
|
|
51
|
+
if index > 0
|
|
52
|
+
this[index - 1] = this[index]
|
|
53
|
+
_length--
|
|
54
|
+
return value
|
|
55
|
+
|
|
56
|
+
fun unshift(...items: T[]): number =
|
|
57
|
+
const added = items.length
|
|
58
|
+
for const index of this.indices
|
|
59
|
+
const reverseIndex = _length - index - 1
|
|
60
|
+
this[reverseIndex + added] = this[reverseIndex]
|
|
61
|
+
for const index of items.indices
|
|
62
|
+
this[index] = items[index]
|
|
63
|
+
_length += added
|
|
64
|
+
return _length
|
|
65
|
+
|
|
66
|
+
fun reverse(): T[] =
|
|
67
|
+
for const index of this.indices
|
|
68
|
+
const opposite = _length - index - 1
|
|
69
|
+
if index < opposite
|
|
70
|
+
const value = this[index]
|
|
71
|
+
this[index] = this[opposite]
|
|
72
|
+
this[opposite] = value
|
|
73
|
+
return this
|
|
74
|
+
|
|
75
|
+
fun sort(compare: ((T, T) -> number)?): T[] =
|
|
76
|
+
const sorted = compare == null ? this.toArray().sort() : this.toArray().sort(compare!)
|
|
77
|
+
for const index of this.indices
|
|
78
|
+
this[index] = sorted[index]
|
|
79
|
+
return this
|
|
80
|
+
|
|
81
|
+
fun fill(value: T, start: number = 0, end: number = this.length): T[] =
|
|
82
|
+
let position = start < 0 ? _length + start : start
|
|
83
|
+
let to = end < 0 ? _length + end : end
|
|
84
|
+
if position < 0
|
|
85
|
+
position = 0
|
|
86
|
+
if to > _length
|
|
87
|
+
to = _length
|
|
88
|
+
for const index of this.indices
|
|
89
|
+
if index >= position & index < to
|
|
90
|
+
this[index] = value
|
|
91
|
+
return this
|
|
92
|
+
|
|
93
|
+
fun slice(start: number = 0, end: number = this.length): T[] =
|
|
94
|
+
let result: T[] = []
|
|
95
|
+
let position = start < 0 ? _length + start : start
|
|
96
|
+
let limit = end < 0 ? _length + end : end
|
|
97
|
+
if position < 0
|
|
98
|
+
position = 0
|
|
99
|
+
if limit > _length
|
|
100
|
+
limit = _length
|
|
101
|
+
for const index of this.indices
|
|
102
|
+
if index >= position & index < limit
|
|
103
|
+
result.push(this[index])
|
|
104
|
+
return result
|
|
105
|
+
|
|
106
|
+
fun copyWithin(target: number, start: number = 0, end: number = this.length): T[] =
|
|
107
|
+
let destination = target < 0 ? _length + target : target
|
|
108
|
+
if destination < 0
|
|
109
|
+
destination = 0
|
|
110
|
+
const values = this.slice(start, end)
|
|
111
|
+
for const index of this.indices
|
|
112
|
+
if index < values.length & destination + index < _length
|
|
113
|
+
this[destination + index] = values[index]
|
|
114
|
+
return this
|
|
115
|
+
|
|
116
|
+
fun splice(start: number, deleteCount: number = this.length - start, ...items: T[]): T[] =
|
|
117
|
+
let position = start < 0 ? _length + start : start
|
|
118
|
+
if position < 0
|
|
119
|
+
position = 0
|
|
120
|
+
if position > _length
|
|
121
|
+
position = _length
|
|
122
|
+
|
|
123
|
+
let count = deleteCount
|
|
124
|
+
if count < 0
|
|
125
|
+
count = 0
|
|
126
|
+
if count > _length - position
|
|
127
|
+
count = _length - position
|
|
128
|
+
|
|
129
|
+
const removed = this.slice(position, position + count)
|
|
130
|
+
const tail = this.slice(position + count, _length)
|
|
131
|
+
_length = position
|
|
132
|
+
items.forEach((item) => this.push(item))
|
|
133
|
+
tail.forEach((item) => this.push(item))
|
|
134
|
+
return removed
|
|
135
|
+
}
|
|
136
|
+
|
|
137
|
+
let index = 0
|
|
138
|
+
while index < length
|
|
139
|
+
let element: T? = null
|
|
140
|
+
if options != null
|
|
141
|
+
element = options!.fill(element, index)
|
|
142
|
+
result.push(element)
|
|
143
|
+
index++
|
|
144
|
+
|
|
145
|
+
return result
|
|
146
|
+
|
|
147
|
+
fun of(...items: T[]): T[] =
|
|
148
|
+
const fill = (_, idx: number): T => items[idx]
|
|
149
|
+
return Array<T>(items.length, { fill })
|
|
150
|
+
|
|
151
|
+
fun from<U>(items: Iterable<U>, mapFn: ((U, number) -> T)?, thisArg: any?): T[] =
|
|
152
|
+
const source = items.toArray()
|
|
153
|
+
const fill = (_, index: number): T =>
|
|
154
|
+
if mapFn == null
|
|
155
|
+
return source[index]
|
|
156
|
+
return mapFn!.call(thisArg, source[index], index)
|
|
157
|
+
return Array<T>(source.length, { fill })
|
|
158
|
+
}
|
package/types/boolean.jn
ADDED
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
import { Math } from 'juno'
|
|
2
|
+
|
|
3
|
+
type BooleanConstructor = {
|
|
4
|
+
fun (value : any) : boolean
|
|
5
|
+
}
|
|
6
|
+
|
|
7
|
+
export const Boolean : BooleanConstructor = {
|
|
8
|
+
fun (value: any): boolean =
|
|
9
|
+
const falsyValues : any[] = [0, -0, 0n, '', null, false, Math.NaN]
|
|
10
|
+
|
|
11
|
+
if falsyValues.includes(value)
|
|
12
|
+
return false
|
|
13
|
+
if value.isType<{ fun toBoolean(): boolean }>()
|
|
14
|
+
return value.toBoolean()
|
|
15
|
+
return true
|
|
16
|
+
fun toString() =
|
|
17
|
+
return 'Boolean'
|
|
18
|
+
}
|
package/types/error.jn
ADDED
|
@@ -0,0 +1,28 @@
|
|
|
1
|
+
import { captureStackTrace } from './error.ts'
|
|
2
|
+
|
|
3
|
+
type ErrorType = {
|
|
4
|
+
readonly type : string
|
|
5
|
+
readonly message : string
|
|
6
|
+
readonly stackTrace : string[]?
|
|
7
|
+
}
|
|
8
|
+
|
|
9
|
+
export constructor Error : ErrorType {
|
|
10
|
+
fun (error : (string | Error)?) =
|
|
11
|
+
if isType<string?>(error)
|
|
12
|
+
return {
|
|
13
|
+
type : 'Error',
|
|
14
|
+
message : error ?? '',
|
|
15
|
+
stackTrace : captureStackTrace()
|
|
16
|
+
}.asReadonly()
|
|
17
|
+
return { ...error }
|
|
18
|
+
|
|
19
|
+
fun result<E is Error>(error : E) : { ok : false, error : E } =
|
|
20
|
+
return { ok : false, error }
|
|
21
|
+
}
|
|
22
|
+
|
|
23
|
+
export type Result<T, E is Error = Error> =
|
|
24
|
+
| { ok : true, value : T }
|
|
25
|
+
| { ok : false, error : E }
|
|
26
|
+
|
|
27
|
+
export fun ok<T>(value : T) : Result<T> =
|
|
28
|
+
return { ok : true, value }
|
package/types/error.ts
ADDED
|
@@ -0,0 +1,9 @@
|
|
|
1
|
+
type StackCapture = () => string[]
|
|
2
|
+
|
|
3
|
+
const captureStackTraceSymbol = Symbol.for('junoscript.captureStackTrace')
|
|
4
|
+
|
|
5
|
+
/** Returns the active JunoScript call stack supplied by the interpreter. */
|
|
6
|
+
export function captureStackTrace(): string[] {
|
|
7
|
+
const capture = (globalThis as typeof globalThis & Record<PropertyKey, unknown>)[captureStackTraceSymbol]
|
|
8
|
+
return typeof capture === 'function' ? [...(capture as StackCapture)()] : []
|
|
9
|
+
}
|
package/types/index.jn
ADDED
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
export import { Iterable } from './iterable'
|
|
2
|
+
export import { Range } from './range'
|
|
3
|
+
export import { Array } from './array'
|
|
4
|
+
export import { Set, ValueSet } from './set'
|
|
5
|
+
export import { Map, ValueMap } from './map'
|
|
6
|
+
export import { Queue } from './queue'
|
|
7
|
+
export import { Object } from './object'
|
|
8
|
+
export import { Boolean } from './boolean'
|
|
9
|
+
export import { Tuple } from './tuple'
|
|
10
|
+
export import { Random } from './random'
|
|
11
|
+
export import { Error, Result, ok } from './error'
|
|
@@ -0,0 +1,211 @@
|
|
|
1
|
+
# Shared Iterable type definition used by JunoScript and the TypeScript build.
|
|
2
|
+
export type Iterable<T> = ({
|
|
3
|
+
iterator(): () -> ({ value: T } | null)
|
|
4
|
+
} | {
|
|
5
|
+
toArray(): T[]
|
|
6
|
+
}) & {
|
|
7
|
+
toArray(): T[] =
|
|
8
|
+
let result: T[] = []
|
|
9
|
+
if this.has("iterator")
|
|
10
|
+
const nextFunction = this.iterator()
|
|
11
|
+
|
|
12
|
+
let next = nextFunction()
|
|
13
|
+
while next != null
|
|
14
|
+
result.push(next.value)
|
|
15
|
+
next = nextFunction()
|
|
16
|
+
else
|
|
17
|
+
return []
|
|
18
|
+
return result
|
|
19
|
+
|
|
20
|
+
get length(): number =
|
|
21
|
+
let _length = 0
|
|
22
|
+
for const item of this.toArray()
|
|
23
|
+
_length++
|
|
24
|
+
return _length
|
|
25
|
+
|
|
26
|
+
at(index: number): T | null =
|
|
27
|
+
let position = index
|
|
28
|
+
let array = this.toArray()
|
|
29
|
+
|
|
30
|
+
if position < 0
|
|
31
|
+
position = this.length + position
|
|
32
|
+
return array[position] ?? null
|
|
33
|
+
|
|
34
|
+
concat(other: T[]): T[] =
|
|
35
|
+
let result: T[] = []
|
|
36
|
+
for const item of this.toArray()
|
|
37
|
+
result.push(item)
|
|
38
|
+
for const item of other
|
|
39
|
+
result.push(item)
|
|
40
|
+
return result
|
|
41
|
+
|
|
42
|
+
every(callback: (T) -> boolean): boolean =
|
|
43
|
+
for const item of this.toArray()
|
|
44
|
+
if !callback(item)
|
|
45
|
+
return false
|
|
46
|
+
return true
|
|
47
|
+
|
|
48
|
+
filter(callback: (T) -> boolean): T[] =
|
|
49
|
+
let result: T[] = []
|
|
50
|
+
for const item of this.toArray()
|
|
51
|
+
if callback(item)
|
|
52
|
+
result.push(item)
|
|
53
|
+
return result
|
|
54
|
+
|
|
55
|
+
flat(): any[] =
|
|
56
|
+
return this.toArray().flat()
|
|
57
|
+
|
|
58
|
+
find(callback: (T) -> boolean): T | null =
|
|
59
|
+
let array = this.toArray()
|
|
60
|
+
|
|
61
|
+
for const item of array
|
|
62
|
+
if callback(item)
|
|
63
|
+
return item
|
|
64
|
+
return null
|
|
65
|
+
|
|
66
|
+
findLast(callback: (T) -> boolean): T | null =
|
|
67
|
+
let index = this.length - 1
|
|
68
|
+
let array = this.toArray()
|
|
69
|
+
|
|
70
|
+
while index >= 0
|
|
71
|
+
let item = array[index]
|
|
72
|
+
if callback(item)
|
|
73
|
+
return item
|
|
74
|
+
index--
|
|
75
|
+
return null
|
|
76
|
+
|
|
77
|
+
flatMap(callback: (T) -> T[]): T[] =
|
|
78
|
+
let result: T[] = []
|
|
79
|
+
for const item of this.toArray()
|
|
80
|
+
for const mapped of callback(item)
|
|
81
|
+
result.push(mapped)
|
|
82
|
+
return result
|
|
83
|
+
|
|
84
|
+
forEach(callback: (T) -> any): void =
|
|
85
|
+
for const item of this.toArray()
|
|
86
|
+
callback(item)
|
|
87
|
+
|
|
88
|
+
includes(value: T): boolean =
|
|
89
|
+
for const item of this.toArray()
|
|
90
|
+
if item == value
|
|
91
|
+
return true
|
|
92
|
+
return false
|
|
93
|
+
|
|
94
|
+
indexOf(value: T): number =
|
|
95
|
+
let index = 0
|
|
96
|
+
for const item of this.toArray()
|
|
97
|
+
if item == value
|
|
98
|
+
return index
|
|
99
|
+
index++
|
|
100
|
+
return -1
|
|
101
|
+
|
|
102
|
+
lastIndexOf(value: T): number =
|
|
103
|
+
let index = this.length - 1
|
|
104
|
+
while index >= 0
|
|
105
|
+
if this.toArray()[index] == value
|
|
106
|
+
return index
|
|
107
|
+
index--
|
|
108
|
+
return -1
|
|
109
|
+
|
|
110
|
+
join(separator: string = ","): string =
|
|
111
|
+
let result = ""
|
|
112
|
+
let first = true
|
|
113
|
+
for const item of this.toArray()
|
|
114
|
+
if !first
|
|
115
|
+
result = result + separator
|
|
116
|
+
result = result + item.toString()
|
|
117
|
+
first = false
|
|
118
|
+
return result
|
|
119
|
+
|
|
120
|
+
map(callback: (T) -> any): any[] =
|
|
121
|
+
let result: any[] = []
|
|
122
|
+
for const item of this.toArray()
|
|
123
|
+
result.push(callback(item))
|
|
124
|
+
return result
|
|
125
|
+
|
|
126
|
+
reduce(callback: (T, T) -> T, initial: T): T =
|
|
127
|
+
let result = initial
|
|
128
|
+
for const item of this.toArray()
|
|
129
|
+
result = callback(result, item)
|
|
130
|
+
return result
|
|
131
|
+
|
|
132
|
+
reduceRight(callback: (T, T) -> T, initial: T): T =
|
|
133
|
+
let result = initial
|
|
134
|
+
let index = this.toArray().length - 1
|
|
135
|
+
while index >= 0
|
|
136
|
+
result = callback(result, this.toArray()[index])
|
|
137
|
+
index = index - 1
|
|
138
|
+
return result
|
|
139
|
+
|
|
140
|
+
slice(start: number = 0, end: number = this.toArray().length): T[] =
|
|
141
|
+
let result: T[] = []
|
|
142
|
+
let index = 0
|
|
143
|
+
let normalizedStart = start
|
|
144
|
+
let normalizedEnd = end
|
|
145
|
+
if normalizedStart < 0
|
|
146
|
+
normalizedStart = this.toArray().length + normalizedStart
|
|
147
|
+
if normalizedEnd < 0
|
|
148
|
+
normalizedEnd = this.toArray().length + normalizedEnd
|
|
149
|
+
for const item of this.toArray()
|
|
150
|
+
if index >= normalizedStart & index < normalizedEnd
|
|
151
|
+
result.push(item)
|
|
152
|
+
index = index + 1
|
|
153
|
+
return result
|
|
154
|
+
|
|
155
|
+
toReversed(): T[] =
|
|
156
|
+
let result: T[] = []
|
|
157
|
+
let index = this.toArray().length - 1
|
|
158
|
+
while index >= 0
|
|
159
|
+
result.push(this.toArray()[index])
|
|
160
|
+
index = index - 1
|
|
161
|
+
return result
|
|
162
|
+
|
|
163
|
+
toSorted(): T[] =
|
|
164
|
+
let result: T[] = []
|
|
165
|
+
for const item of this.toArray()
|
|
166
|
+
result.push(item)
|
|
167
|
+
return result.sort()
|
|
168
|
+
|
|
169
|
+
toLocaleString(): string =
|
|
170
|
+
return this.toArray().toLocaleString()
|
|
171
|
+
|
|
172
|
+
toString(): string =
|
|
173
|
+
return this.toArray().toString()
|
|
174
|
+
|
|
175
|
+
toSpliced(start: number, deleteCount: number, items: T[] = []): T[] =
|
|
176
|
+
let result: T[] = []
|
|
177
|
+
let index = 0
|
|
178
|
+
let insertAt = start
|
|
179
|
+
let inserted = false
|
|
180
|
+
if insertAt < 0
|
|
181
|
+
insertAt = this.toArray().length + insertAt
|
|
182
|
+
for const item of this.toArray()
|
|
183
|
+
if !inserted & index >= insertAt
|
|
184
|
+
for const insertion of items
|
|
185
|
+
result.push(insertion)
|
|
186
|
+
inserted = true
|
|
187
|
+
if index < insertAt | index >= insertAt + deleteCount
|
|
188
|
+
result.push(item)
|
|
189
|
+
index = index + 1
|
|
190
|
+
if !inserted
|
|
191
|
+
for const insertion of items
|
|
192
|
+
result.push(insertion)
|
|
193
|
+
return result
|
|
194
|
+
|
|
195
|
+
with(index: number, value: T): T[] =
|
|
196
|
+
let result: T[] = []
|
|
197
|
+
let position = 0
|
|
198
|
+
for const item of this.toArray()
|
|
199
|
+
if position == index
|
|
200
|
+
result.push(value)
|
|
201
|
+
else
|
|
202
|
+
result.push(item)
|
|
203
|
+
position = position + 1
|
|
204
|
+
return result
|
|
205
|
+
|
|
206
|
+
some(callback: (T) -> boolean): boolean =
|
|
207
|
+
for const item of this.toArray()
|
|
208
|
+
if callback(item)
|
|
209
|
+
return true
|
|
210
|
+
return false
|
|
211
|
+
}
|
package/types/map.jn
ADDED
|
@@ -0,0 +1,44 @@
|
|
|
1
|
+
import { Iterable } from './iterable'
|
|
2
|
+
import { globalMap, globalMapToObject, globalValueMap } from './map.ts'
|
|
3
|
+
|
|
4
|
+
export constructor Map<K, V> : Iterable<[K, V]> & {
|
|
5
|
+
readonly length: number
|
|
6
|
+
set(key: K, value: V): Map<K, V>
|
|
7
|
+
get(key: K): V | null
|
|
8
|
+
hasKey(key: K): boolean
|
|
9
|
+
delete(key: K): boolean
|
|
10
|
+
clear(): void
|
|
11
|
+
mapKeys(): K[]
|
|
12
|
+
values(): V[]
|
|
13
|
+
iterator(): () -> ({ value: [K, V] } | null)
|
|
14
|
+
} {
|
|
15
|
+
fun (...entries: [K, V][]) =
|
|
16
|
+
return globalMap<K, V>(...entries)
|
|
17
|
+
|
|
18
|
+
fun from(entries: Iterable<[K, V]>): Map<K, V> =
|
|
19
|
+
return Map<K, V>(...entries.toArray())
|
|
20
|
+
|
|
21
|
+
fun toObject(map: Map<string | number, any>): { [key: string | number]: any } =
|
|
22
|
+
return globalMapToObject(map)
|
|
23
|
+
}
|
|
24
|
+
|
|
25
|
+
export constructor ValueMap<K, V> : Iterable<[K, V]> & {
|
|
26
|
+
readonly length: number
|
|
27
|
+
set(key: K, value: V): ValueMap<K, V>
|
|
28
|
+
get(key: K): V | null
|
|
29
|
+
hasKey(key: K): boolean
|
|
30
|
+
delete(key: K): boolean
|
|
31
|
+
clear(): void
|
|
32
|
+
mapKeys(): K[]
|
|
33
|
+
values(): V[]
|
|
34
|
+
iterator(): () -> ({ value: [K, V] } | null)
|
|
35
|
+
} {
|
|
36
|
+
fun (...entries: [K, V][]) =
|
|
37
|
+
return globalValueMap<K, V>(...entries)
|
|
38
|
+
|
|
39
|
+
fun from(entries: Iterable<[K, V]>): ValueMap<K, V> =
|
|
40
|
+
return ValueMap<K, V>(...entries.toArray())
|
|
41
|
+
|
|
42
|
+
fun toObject(map: ValueMap<string | number, any>): { [key: string | number]: any } =
|
|
43
|
+
return globalMapToObject(map)
|
|
44
|
+
}
|
package/types/map.ts
ADDED
|
@@ -0,0 +1,132 @@
|
|
|
1
|
+
import { immutableSnapshot, junoEqual, structuralHash } from './set.ts'
|
|
2
|
+
|
|
3
|
+
export type JunoMap<K, V> = {
|
|
4
|
+
readonly length: number
|
|
5
|
+
set(key: K, value: V): JunoMap<K, V>
|
|
6
|
+
get(key: K): V | null
|
|
7
|
+
hasKey(key: K): boolean
|
|
8
|
+
delete(key: K): boolean
|
|
9
|
+
clear(): void
|
|
10
|
+
mapKeys(): K[]
|
|
11
|
+
values(): V[]
|
|
12
|
+
iterator(): () => ({ value: [K, V] } | null)
|
|
13
|
+
}
|
|
14
|
+
|
|
15
|
+
export function globalMap<K, V>(...initialEntries: [K, V][]): JunoMap<K, V> {
|
|
16
|
+
type Entry = { key: K; value: V; hash: string; active: boolean }
|
|
17
|
+
const order: Entry[] = []
|
|
18
|
+
const buckets = new globalThis.Map<string, globalThis.Set<Entry>>()
|
|
19
|
+
const identities = new WeakMap<object, Entry>()
|
|
20
|
+
let activeLength = 0
|
|
21
|
+
|
|
22
|
+
const addToBucket = (entry: Entry): void => {
|
|
23
|
+
let bucket = buckets.get(entry.hash)
|
|
24
|
+
if (!bucket) buckets.set(entry.hash, bucket = new globalThis.Set<Entry>())
|
|
25
|
+
bucket.add(entry)
|
|
26
|
+
}
|
|
27
|
+
const removeFromBucket = (entry: Entry): void => {
|
|
28
|
+
const bucket = buckets.get(entry.hash)
|
|
29
|
+
bucket?.delete(entry)
|
|
30
|
+
if (bucket?.size === 0) buckets.delete(entry.hash)
|
|
31
|
+
}
|
|
32
|
+
const refreshIdentity = (key: K): Entry | undefined => {
|
|
33
|
+
if ((typeof key !== 'object' || key === null) && typeof key !== 'function') return undefined
|
|
34
|
+
const entry = identities.get(key as object)
|
|
35
|
+
if (!entry?.active) return undefined
|
|
36
|
+
const hash = structuralHash(key)
|
|
37
|
+
if (hash !== entry.hash) {
|
|
38
|
+
removeFromBucket(entry)
|
|
39
|
+
entry.hash = hash
|
|
40
|
+
addToBucket(entry)
|
|
41
|
+
}
|
|
42
|
+
return entry
|
|
43
|
+
}
|
|
44
|
+
const find = (key: K): Entry | undefined => {
|
|
45
|
+
const identical = refreshIdentity(key)
|
|
46
|
+
if (identical) return identical
|
|
47
|
+
const bucket = buckets.get(structuralHash(key))
|
|
48
|
+
if (!bucket) return undefined
|
|
49
|
+
for (const entry of bucket) if (entry.active && junoEqual(entry.key, key)) return entry
|
|
50
|
+
return undefined
|
|
51
|
+
}
|
|
52
|
+
|
|
53
|
+
const result: JunoMap<K, V> = {
|
|
54
|
+
get length(): number { return activeLength },
|
|
55
|
+
set(key: K, value: V): JunoMap<K, V> {
|
|
56
|
+
const existing = find(key)
|
|
57
|
+
if (existing) {
|
|
58
|
+
existing.value = value
|
|
59
|
+
return result
|
|
60
|
+
}
|
|
61
|
+
const entry: Entry = { key, value, hash: structuralHash(key), active: true }
|
|
62
|
+
order.push(entry)
|
|
63
|
+
addToBucket(entry)
|
|
64
|
+
if ((typeof key === 'object' && key !== null) || typeof key === 'function') identities.set(key as object, entry)
|
|
65
|
+
activeLength++
|
|
66
|
+
return result
|
|
67
|
+
},
|
|
68
|
+
get(key: K): V | null { return find(key)?.value ?? null },
|
|
69
|
+
hasKey(key: K): boolean { return find(key) !== undefined },
|
|
70
|
+
delete(key: K): boolean {
|
|
71
|
+
const entry = find(key)
|
|
72
|
+
if (!entry) return false
|
|
73
|
+
entry.active = false
|
|
74
|
+
removeFromBucket(entry)
|
|
75
|
+
if ((typeof entry.key === 'object' && entry.key !== null) || typeof entry.key === 'function') identities.delete(entry.key as object)
|
|
76
|
+
activeLength--
|
|
77
|
+
return true
|
|
78
|
+
},
|
|
79
|
+
clear(): void {
|
|
80
|
+
for (const entry of order) entry.active = false
|
|
81
|
+
order.length = 0
|
|
82
|
+
buckets.clear()
|
|
83
|
+
activeLength = 0
|
|
84
|
+
},
|
|
85
|
+
mapKeys(): K[] { return order.filter(entry => entry.active).map(entry => entry.key) },
|
|
86
|
+
values(): V[] { return order.filter(entry => entry.active).map(entry => entry.value) },
|
|
87
|
+
iterator(): () => ({ value: [K, V] } | null) {
|
|
88
|
+
let index = 0
|
|
89
|
+
return () => {
|
|
90
|
+
while (index < order.length) {
|
|
91
|
+
const entry = order[index++]
|
|
92
|
+
if (entry.active) return { value: [entry.key, entry.value] }
|
|
93
|
+
}
|
|
94
|
+
return null
|
|
95
|
+
}
|
|
96
|
+
}
|
|
97
|
+
}
|
|
98
|
+
for (const [key, value] of initialEntries) result.set(key, value)
|
|
99
|
+
return result
|
|
100
|
+
}
|
|
101
|
+
|
|
102
|
+
export function globalMapToObject(map: JunoMap<string | number, unknown>): Record<string, unknown> {
|
|
103
|
+
const result: Record<string, unknown> = Object.create(null)
|
|
104
|
+
const next = map.iterator()
|
|
105
|
+
for (let entry = next(); entry !== null; entry = next()) {
|
|
106
|
+
const [key, value] = entry.value
|
|
107
|
+
Object.defineProperty(result, String(key), { value, enumerable: true, configurable: true, writable: true })
|
|
108
|
+
}
|
|
109
|
+
return result
|
|
110
|
+
}
|
|
111
|
+
|
|
112
|
+
/** A structural map that stores immutable key snapshots, keeping hashes stable. */
|
|
113
|
+
export function globalValueMap<K, V>(...initialEntries: [K, V][]): JunoMap<K, V> {
|
|
114
|
+
const storage = globalMap<K, V>()
|
|
115
|
+
const result: JunoMap<K, V> = {
|
|
116
|
+
get length(): number { return storage.length },
|
|
117
|
+
set(key: K, value: V): JunoMap<K, V> {
|
|
118
|
+
if (storage.hasKey(key)) storage.set(key, value)
|
|
119
|
+
else storage.set(immutableSnapshot(key), value)
|
|
120
|
+
return result
|
|
121
|
+
},
|
|
122
|
+
get(key: K): V | null { return storage.get(key) },
|
|
123
|
+
hasKey(key: K): boolean { return storage.hasKey(key) },
|
|
124
|
+
delete(key: K): boolean { return storage.delete(key) },
|
|
125
|
+
clear(): void { storage.clear() },
|
|
126
|
+
mapKeys(): K[] { return storage.mapKeys() },
|
|
127
|
+
values(): V[] { return storage.values() },
|
|
128
|
+
iterator(): () => ({ value: [K, V] } | null) { return storage.iterator() }
|
|
129
|
+
}
|
|
130
|
+
for (const [key, value] of initialEntries) result.set(key, value)
|
|
131
|
+
return result
|
|
132
|
+
}
|
package/types/number.ts
ADDED
|
@@ -0,0 +1,52 @@
|
|
|
1
|
+
const {
|
|
2
|
+
EPSILON,
|
|
3
|
+
MAX_SAFE_INTEGER,
|
|
4
|
+
MAX_VALUE,
|
|
5
|
+
MIN_SAFE_INTEGER,
|
|
6
|
+
MIN_VALUE,
|
|
7
|
+
NEGATIVE_INFINITY,
|
|
8
|
+
POSITIVE_INFINITY,
|
|
9
|
+
NaN,
|
|
10
|
+
isFinite,
|
|
11
|
+
isInteger,
|
|
12
|
+
isNaN,
|
|
13
|
+
isSafeInteger,
|
|
14
|
+
parseFloat,
|
|
15
|
+
parseInt
|
|
16
|
+
} = globalThis.Number
|
|
17
|
+
|
|
18
|
+
const NumberConstructor = Object.assign(
|
|
19
|
+
(value ?: any) : number => {
|
|
20
|
+
if (value == null) {
|
|
21
|
+
return NaN;
|
|
22
|
+
}
|
|
23
|
+
if (value && typeof value === 'object') {
|
|
24
|
+
if ('valueOf' in value && typeof value.valueOf === 'function') {
|
|
25
|
+
const _valueOf = value.valueOf()
|
|
26
|
+
|
|
27
|
+
return globalThis.Number(_valueOf)
|
|
28
|
+
}
|
|
29
|
+
return NaN;
|
|
30
|
+
}
|
|
31
|
+
|
|
32
|
+
return globalThis.Number(value)
|
|
33
|
+
},
|
|
34
|
+
{
|
|
35
|
+
EPSILON,
|
|
36
|
+
MAX_SAFE_INTEGER,
|
|
37
|
+
MAX_VALUE,
|
|
38
|
+
MIN_SAFE_INTEGER,
|
|
39
|
+
MIN_VALUE,
|
|
40
|
+
NEGATIVE_INFINITY,
|
|
41
|
+
POSITIVE_INFINITY,
|
|
42
|
+
NaN,
|
|
43
|
+
isFinite,
|
|
44
|
+
isInteger,
|
|
45
|
+
isNaN,
|
|
46
|
+
isSafeInteger,
|
|
47
|
+
parseFloat,
|
|
48
|
+
parseInt
|
|
49
|
+
}
|
|
50
|
+
)
|
|
51
|
+
|
|
52
|
+
export const Number = NumberConstructor;
|