@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/types/object.jn
ADDED
|
@@ -0,0 +1,94 @@
|
|
|
1
|
+
import { stringify, parse, nativeKeys, readValue, clone, freeze } from './object.ts'
|
|
2
|
+
import { KeyOf, Omit, Pick, Readonly } from 'juno:types'
|
|
3
|
+
|
|
4
|
+
type AnyObject = { [key: string | number]: any }
|
|
5
|
+
|
|
6
|
+
type ObjectMethods<T is object> = {
|
|
7
|
+
sealed pick<Keys is KeyOf<T>>(...keys: Keys[]): Pick<T, Keys>
|
|
8
|
+
sealed omit<Keys is KeyOf<T>>(...keys: Keys[]): Omit<T, Keys>
|
|
9
|
+
sealed keys(): KeyOf<T>[]
|
|
10
|
+
sealed values(): T[KeyOf<T>][]
|
|
11
|
+
sealed entries(): [KeyOf<T>, T[KeyOf<T>]][]
|
|
12
|
+
sealed has(key: string | number): boolean
|
|
13
|
+
sealed asReadonly(): Readonly<T>
|
|
14
|
+
sealed asMutable(): T
|
|
15
|
+
sealed deepClone(): T
|
|
16
|
+
sealed deepFreeze(): Readonly<T>
|
|
17
|
+
sealed toDeepFreezed(): Readonly<T>
|
|
18
|
+
sealed toJSON(): T
|
|
19
|
+
sealed toJson(): T
|
|
20
|
+
sealed toString(): string
|
|
21
|
+
}
|
|
22
|
+
|
|
23
|
+
export constructor Object<T is object = object> {
|
|
24
|
+
fun (obj: T): T & ObjectMethods<T> =
|
|
25
|
+
const objectKeys = nativeKeys(obj)
|
|
26
|
+
return {
|
|
27
|
+
...obj,
|
|
28
|
+
|
|
29
|
+
sealed fun pick<K is KeyOf<T>>(...keys: K[]): { [key : K] : typeof<this[key]> } =
|
|
30
|
+
let result: { [key : K] : typeof<this[key]>? } = {}
|
|
31
|
+
for const key of keys
|
|
32
|
+
result[key] = readValue(obj, key)
|
|
33
|
+
return result
|
|
34
|
+
|
|
35
|
+
sealed fun omit<K is KeyOf<T>>(...keys: K[]): Omit<T, K> =
|
|
36
|
+
let result: { [key : KeyOf<T>]: typeof<this[key]>? } = {}
|
|
37
|
+
for const entry of this.entries()
|
|
38
|
+
if !keys.includes(entry[0])
|
|
39
|
+
result[entry[0]] = entry[1]
|
|
40
|
+
return result
|
|
41
|
+
|
|
42
|
+
sealed fun keys(): KeyOf<T>[] =
|
|
43
|
+
return objectKeys
|
|
44
|
+
|
|
45
|
+
sealed fun values(): T[KeyOf<T>][] =
|
|
46
|
+
return this.keys().map((key) => readValue(obj, key))
|
|
47
|
+
|
|
48
|
+
sealed fun entries(): [KeyOf<T>, T[KeyOf<T>]][] =
|
|
49
|
+
return this.keys().map((key) => [key, readValue(obj, key)])
|
|
50
|
+
|
|
51
|
+
sealed fun has(key: string | number): boolean =
|
|
52
|
+
return this.keys().includes(key)
|
|
53
|
+
|
|
54
|
+
sealed fun asReadonly(): Readonly<T> =
|
|
55
|
+
return freeze(clone(this))
|
|
56
|
+
|
|
57
|
+
sealed fun asMutable(): T =
|
|
58
|
+
return clone(this)
|
|
59
|
+
|
|
60
|
+
sealed fun deepClone(): T =
|
|
61
|
+
return clone(this)
|
|
62
|
+
|
|
63
|
+
sealed fun deepFreeze(): Readonly<T> =
|
|
64
|
+
return freeze(this)
|
|
65
|
+
|
|
66
|
+
sealed fun toDeepFreezed(): Readonly<T> =
|
|
67
|
+
return freeze(clone(this))
|
|
68
|
+
|
|
69
|
+
sealed fun toJSON(): T =
|
|
70
|
+
let result: { [key : KeyOf<T>]: typeof<this[key]>? } = {}
|
|
71
|
+
for const key of objectKeys
|
|
72
|
+
result[key] = readValue(obj, key)
|
|
73
|
+
return result
|
|
74
|
+
|
|
75
|
+
sealed fun toJson(): T =
|
|
76
|
+
return this.toJSON()
|
|
77
|
+
|
|
78
|
+
sealed fun toString(): string =
|
|
79
|
+
return stringify(this.toJSON())
|
|
80
|
+
}
|
|
81
|
+
|
|
82
|
+
fun from<K is string | number, V>(entries: Iterable<[K, V]>): Object<{ [key: K]: V }> =
|
|
83
|
+
let result: { [key: K]: V } = {}
|
|
84
|
+
for const entry of entries
|
|
85
|
+
result[entry[0]] = entry[1]
|
|
86
|
+
return Object<object>(result)
|
|
87
|
+
|
|
88
|
+
fun fromMap<K is string | number, V>(map: Map<K, V>): Object<{ [key: K]: V }> =
|
|
89
|
+
return Object.from(map)
|
|
90
|
+
|
|
91
|
+
fun fromJSON(json: string): Object<AnyObject> =
|
|
92
|
+
const parsedObj = parse(json)
|
|
93
|
+
return Object<object>(parsedObj)
|
|
94
|
+
}
|
package/types/object.ts
ADDED
|
@@ -0,0 +1,49 @@
|
|
|
1
|
+
export const stringify = JSON.stringify
|
|
2
|
+
|
|
3
|
+
export const parse = JSON.parse
|
|
4
|
+
|
|
5
|
+
export const nativeKeys = (value: object): (string | number)[] => Object.keys(value).filter(key =>
|
|
6
|
+
!(key === 'toString'
|
|
7
|
+
&& typeof (value as any)[key] === 'function'
|
|
8
|
+
&& (value as any)[key].__junoDefaultToString)
|
|
9
|
+
)
|
|
10
|
+
|
|
11
|
+
export const readValue = (value: object, key: string | number): any =>
|
|
12
|
+
(value as Record<string | number, any>)[key]
|
|
13
|
+
|
|
14
|
+
export function clone<T>(value: T, seen = new WeakMap<object, any>()): T {
|
|
15
|
+
if (value === null || typeof value !== 'object' || (value as any).__junoFunction || (value as any).__junoConstructor) return value
|
|
16
|
+
const existing = seen.get(value)
|
|
17
|
+
if (existing) return existing
|
|
18
|
+
if (value instanceof Set) {
|
|
19
|
+
const copy = new Set()
|
|
20
|
+
seen.set(value, copy)
|
|
21
|
+
for (const item of value) copy.add(clone(item, seen))
|
|
22
|
+
return copy as T
|
|
23
|
+
}
|
|
24
|
+
if (value instanceof Map) {
|
|
25
|
+
const copy = new Map()
|
|
26
|
+
seen.set(value, copy)
|
|
27
|
+
for (const [key, item] of value) copy.set(clone(key, seen), clone(item, seen))
|
|
28
|
+
return copy as T
|
|
29
|
+
}
|
|
30
|
+
const copy: any = Array.isArray(value) ? [] : Object.create(Object.getPrototypeOf(value))
|
|
31
|
+
seen.set(value, copy)
|
|
32
|
+
for (const key of Reflect.ownKeys(value)) {
|
|
33
|
+
const descriptor = Object.getOwnPropertyDescriptor(value, key)
|
|
34
|
+
if (!descriptor) continue
|
|
35
|
+
if ('value' in descriptor) descriptor.value = clone(descriptor.value, seen)
|
|
36
|
+
Object.defineProperty(copy, key, descriptor)
|
|
37
|
+
}
|
|
38
|
+
return copy
|
|
39
|
+
}
|
|
40
|
+
|
|
41
|
+
export function freeze<T>(value: T, seen = new WeakSet<object>()): T {
|
|
42
|
+
if (value === null || typeof value !== 'object' || (value as any).__junoFunction || (value as any).__junoConstructor || seen.has(value)) return value
|
|
43
|
+
seen.add(value)
|
|
44
|
+
for (const key of Reflect.ownKeys(value)) {
|
|
45
|
+
const descriptor = Object.getOwnPropertyDescriptor(value, key)
|
|
46
|
+
if (descriptor && 'value' in descriptor) freeze(descriptor.value, seen)
|
|
47
|
+
}
|
|
48
|
+
return Object.freeze(value)
|
|
49
|
+
}
|
package/types/queue.jn
ADDED
|
@@ -0,0 +1,46 @@
|
|
|
1
|
+
import { Iterable } from './iterable'
|
|
2
|
+
import { Range } from './range'
|
|
3
|
+
import { Array } from './array'
|
|
4
|
+
|
|
5
|
+
export constructor Queue<T> : Iterable<T> {
|
|
6
|
+
fun (...items : T[]) =
|
|
7
|
+
|
|
8
|
+
let _start = 0
|
|
9
|
+
|
|
10
|
+
let result = {
|
|
11
|
+
get length(): number =
|
|
12
|
+
return items.length - _start
|
|
13
|
+
|
|
14
|
+
fun copy(): Queue<T> =
|
|
15
|
+
return Queue(...items.slice(_start))
|
|
16
|
+
|
|
17
|
+
fun toArray(): T[] =
|
|
18
|
+
return items.slice(_start)
|
|
19
|
+
|
|
20
|
+
fun push(...newItems: T[]): number =
|
|
21
|
+
return items.push(...newItems)
|
|
22
|
+
|
|
23
|
+
fun pop(): T? =
|
|
24
|
+
if this.length == 0
|
|
25
|
+
return null
|
|
26
|
+
const value = items[_start]
|
|
27
|
+
++_start
|
|
28
|
+
return value
|
|
29
|
+
|
|
30
|
+
fun copyWithin(target: number, start: number = 0, end: number = this.length): T[] =
|
|
31
|
+
let destination = target < 0 ? this.length + target : target
|
|
32
|
+
if destination < 0
|
|
33
|
+
destination = 0
|
|
34
|
+
|
|
35
|
+
const array = this.toArray()
|
|
36
|
+
const values = array.slice(start, end)
|
|
37
|
+
for const index of array.indices
|
|
38
|
+
if index < values.length & destination + index < this.length
|
|
39
|
+
this[destination + index] = values[index]
|
|
40
|
+
return this
|
|
41
|
+
|
|
42
|
+
}
|
|
43
|
+
|
|
44
|
+
return result
|
|
45
|
+
|
|
46
|
+
}
|
package/types/random.jn
ADDED
|
@@ -0,0 +1,25 @@
|
|
|
1
|
+
import { random } from './random.ts'
|
|
2
|
+
import { Math } from 'juno'
|
|
3
|
+
|
|
4
|
+
type RandomType = {
|
|
5
|
+
|
|
6
|
+
fun next(start: number?, end: number?) : number =
|
|
7
|
+
const randomValue = random()
|
|
8
|
+
|
|
9
|
+
if start == null & end == null
|
|
10
|
+
return randomValue
|
|
11
|
+
|
|
12
|
+
if end == null
|
|
13
|
+
end = start
|
|
14
|
+
start = 0
|
|
15
|
+
|
|
16
|
+
return randomValue * Math.abs(end! - start!) + Math.min(start, end)
|
|
17
|
+
|
|
18
|
+
fun nextInt(start: number?, end: number?) : number =
|
|
19
|
+
const randomValue = this.next(start, end)
|
|
20
|
+
|
|
21
|
+
return Math.floor(randomValue)
|
|
22
|
+
|
|
23
|
+
}
|
|
24
|
+
|
|
25
|
+
export const Random: RandomType = RandomType()
|
package/types/random.ts
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export const random = () => globalThis.Math.random()
|
package/types/range.jn
ADDED
|
@@ -0,0 +1,27 @@
|
|
|
1
|
+
export constructor Range : Iterable<number> {
|
|
2
|
+
fun (start : number, end : number?, step : number = 1) =
|
|
3
|
+
if !end.isType<number>()
|
|
4
|
+
end = start
|
|
5
|
+
start = 0
|
|
6
|
+
|
|
7
|
+
return {
|
|
8
|
+
...Iterable,
|
|
9
|
+
readonly start,
|
|
10
|
+
readonly end,
|
|
11
|
+
readonly step,
|
|
12
|
+
fun iterator() =
|
|
13
|
+
let cur = this.start
|
|
14
|
+
|
|
15
|
+
const { end, start, step } = this
|
|
16
|
+
|
|
17
|
+
return () =>
|
|
18
|
+
const value = cur
|
|
19
|
+
|
|
20
|
+
if (step > 0 & value >= end) | (step < 0 & value <= end)
|
|
21
|
+
return null
|
|
22
|
+
|
|
23
|
+
cur += step
|
|
24
|
+
|
|
25
|
+
return { value }
|
|
26
|
+
}
|
|
27
|
+
}
|
package/types/set.jn
ADDED
|
@@ -0,0 +1,139 @@
|
|
|
1
|
+
import { Iterable } from './iterable'
|
|
2
|
+
import { globalSet, globalValueSet } from './set.ts'
|
|
3
|
+
|
|
4
|
+
export constructor Set<T> : Iterable<T> & {
|
|
5
|
+
readonly length: number
|
|
6
|
+
includes(value: T): boolean
|
|
7
|
+
add(value: T): Set<T>
|
|
8
|
+
delete(value: T): boolean
|
|
9
|
+
clear(): void
|
|
10
|
+
iterator(): () -> ({ value: T } | null)
|
|
11
|
+
union(other: Iterable<T>): Set<T> =
|
|
12
|
+
const result = Set<T>(...this.toArray())
|
|
13
|
+
for const item of other
|
|
14
|
+
result.add(item)
|
|
15
|
+
return result
|
|
16
|
+
|
|
17
|
+
intersection(other: Iterable<T>): Set<T> =
|
|
18
|
+
const result = Set<T>()
|
|
19
|
+
const lookup = Set<T>(...other.toArray())
|
|
20
|
+
for const item of this
|
|
21
|
+
if lookup.includes(item)
|
|
22
|
+
result.add(item)
|
|
23
|
+
return result
|
|
24
|
+
|
|
25
|
+
subtract(other: Iterable<T>): Set<T> =
|
|
26
|
+
const result = Set<T>()
|
|
27
|
+
const lookup = Set<T>(...other.toArray())
|
|
28
|
+
for const item of this
|
|
29
|
+
if !lookup.includes(item)
|
|
30
|
+
result.add(item)
|
|
31
|
+
return result
|
|
32
|
+
|
|
33
|
+
symmetricDifference(other: Iterable<T>): Set<T> =
|
|
34
|
+
const lookup = Set<T>(...other.toArray())
|
|
35
|
+
const result = Set<T>()
|
|
36
|
+
for const item of this
|
|
37
|
+
if !lookup.includes(item)
|
|
38
|
+
result.add(item)
|
|
39
|
+
for const item of other
|
|
40
|
+
if !this.includes(item)
|
|
41
|
+
result.add(item)
|
|
42
|
+
return result
|
|
43
|
+
|
|
44
|
+
isSubset(other: Iterable<T>): boolean =
|
|
45
|
+
const lookup = Set<T>(...other.toArray())
|
|
46
|
+
for const item of this
|
|
47
|
+
if !lookup.includes(item)
|
|
48
|
+
return false
|
|
49
|
+
return true
|
|
50
|
+
|
|
51
|
+
isSuperset(other: Iterable<T>): boolean =
|
|
52
|
+
for const item of other
|
|
53
|
+
if !this.includes(item)
|
|
54
|
+
return false
|
|
55
|
+
return true
|
|
56
|
+
|
|
57
|
+
isDisjoint(other: Iterable<T>): boolean =
|
|
58
|
+
const lookup = Set<T>(...other.toArray())
|
|
59
|
+
for const item of this
|
|
60
|
+
if lookup.includes(item)
|
|
61
|
+
return false
|
|
62
|
+
return true
|
|
63
|
+
} {
|
|
64
|
+
fun (...items: T[]) =
|
|
65
|
+
return globalSet<T>(...items)
|
|
66
|
+
|
|
67
|
+
fun from(items: Iterable<T>): Set<T> =
|
|
68
|
+
return Set<T>(...items.toArray())
|
|
69
|
+
|
|
70
|
+
fun union(left: Iterable<T>, right: Iterable<T>): Set<T> =
|
|
71
|
+
const result = Set<T>(...left.toArray())
|
|
72
|
+
for const item of right
|
|
73
|
+
result.add(item)
|
|
74
|
+
return result
|
|
75
|
+
|
|
76
|
+
fun intersection(left: Iterable<T>, right: Iterable<T>): Set<T> =
|
|
77
|
+
const result = Set<T>()
|
|
78
|
+
const lookup = Set<T>(...right.toArray())
|
|
79
|
+
for const item of left
|
|
80
|
+
if lookup.includes(item)
|
|
81
|
+
result.add(item)
|
|
82
|
+
return result
|
|
83
|
+
|
|
84
|
+
fun subtract(left: Iterable<T>, right: Iterable<T>): Set<T> =
|
|
85
|
+
const result = Set<T>()
|
|
86
|
+
const lookup = Set<T>(...right.toArray())
|
|
87
|
+
for const item of left
|
|
88
|
+
if !lookup.includes(item)
|
|
89
|
+
result.add(item)
|
|
90
|
+
return result
|
|
91
|
+
|
|
92
|
+
fun symmetricDifference(left: Iterable<T>, right: Iterable<T>): Set<T> =
|
|
93
|
+
const result = Set<T>()
|
|
94
|
+
const leftSet = Set<T>(...left.toArray())
|
|
95
|
+
const rightSet = Set<T>(...right.toArray())
|
|
96
|
+
for const item of left
|
|
97
|
+
if !rightSet.includes(item)
|
|
98
|
+
result.add(item)
|
|
99
|
+
for const item of right
|
|
100
|
+
if !leftSet.includes(item)
|
|
101
|
+
result.add(item)
|
|
102
|
+
return result
|
|
103
|
+
|
|
104
|
+
fun isSubset(left: Iterable<T>, right: Iterable<T>): boolean =
|
|
105
|
+
const lookup = Set<T>(...right.toArray())
|
|
106
|
+
for const item of left
|
|
107
|
+
if !lookup.includes(item)
|
|
108
|
+
return false
|
|
109
|
+
return true
|
|
110
|
+
|
|
111
|
+
fun isSuperset(left: Iterable<T>, right: Iterable<T>): boolean =
|
|
112
|
+
const lookup = Set<T>(...left.toArray())
|
|
113
|
+
for const item of right
|
|
114
|
+
if !lookup.includes(item)
|
|
115
|
+
return false
|
|
116
|
+
return true
|
|
117
|
+
|
|
118
|
+
fun isDisjoint(left: Iterable<T>, right: Iterable<T>): boolean =
|
|
119
|
+
const lookup = Set<T>(...right.toArray())
|
|
120
|
+
for const item of left
|
|
121
|
+
if lookup.includes(item)
|
|
122
|
+
return false
|
|
123
|
+
return true
|
|
124
|
+
}
|
|
125
|
+
|
|
126
|
+
export constructor ValueSet<T> : Iterable<T> & {
|
|
127
|
+
readonly length: number
|
|
128
|
+
includes(value: T): boolean
|
|
129
|
+
add(value: T): ValueSet<T>
|
|
130
|
+
delete(value: T): boolean
|
|
131
|
+
clear(): void
|
|
132
|
+
iterator(): () -> ({ value: T } | null)
|
|
133
|
+
} {
|
|
134
|
+
fun (...items: T[]) =
|
|
135
|
+
return globalValueSet<T>(...items)
|
|
136
|
+
|
|
137
|
+
fun from(items: Iterable<T>): ValueSet<T> =
|
|
138
|
+
return ValueSet<T>(...items.toArray())
|
|
139
|
+
}
|
package/types/set.ts
ADDED
|
@@ -0,0 +1,209 @@
|
|
|
1
|
+
export type JunoSet<T> = {
|
|
2
|
+
readonly length: number
|
|
3
|
+
includes(value: T): boolean
|
|
4
|
+
add(value: T): JunoSet<T>
|
|
5
|
+
delete(value: T): boolean
|
|
6
|
+
clear(): void
|
|
7
|
+
iterator(): () => ({ value: T } | null)
|
|
8
|
+
}
|
|
9
|
+
|
|
10
|
+
/** SameValueZero for primitives and structural equality for Juno objects. */
|
|
11
|
+
export function junoEqual(left: unknown, right: unknown, seen = new WeakMap<object, WeakSet<object>>()): boolean {
|
|
12
|
+
if (left === right) return true
|
|
13
|
+
if (typeof left === 'number' && typeof right === 'number' && Number.isNaN(left) && Number.isNaN(right)) return true
|
|
14
|
+
if (left === null || right === null || typeof left !== 'object' || typeof right !== 'object') return false
|
|
15
|
+
if (Array.isArray(left) !== Array.isArray(right)) return false
|
|
16
|
+
|
|
17
|
+
let compared = seen.get(left)
|
|
18
|
+
if (compared?.has(right)) return true
|
|
19
|
+
if (!compared) {
|
|
20
|
+
compared = new WeakSet<object>()
|
|
21
|
+
seen.set(left, compared)
|
|
22
|
+
}
|
|
23
|
+
compared.add(right)
|
|
24
|
+
|
|
25
|
+
const leftRecord = left as Record<string, unknown>
|
|
26
|
+
const rightRecord = right as Record<string, unknown>
|
|
27
|
+
const structuralKeys = (record: Record<string, unknown>): string[] => Object.keys(record).filter(key =>
|
|
28
|
+
!(key === 'toString' && typeof record[key] === 'function' && (record[key] as { __junoDefaultToString?: boolean }).__junoDefaultToString)
|
|
29
|
+
)
|
|
30
|
+
const leftKeys = structuralKeys(leftRecord)
|
|
31
|
+
const rightKeys = structuralKeys(rightRecord)
|
|
32
|
+
return leftKeys.length === rightKeys.length && leftKeys.every(key =>
|
|
33
|
+
Object.prototype.hasOwnProperty.call(rightRecord, key) &&
|
|
34
|
+
junoEqual(leftRecord[key], rightRecord[key], seen)
|
|
35
|
+
)
|
|
36
|
+
}
|
|
37
|
+
|
|
38
|
+
const functionIds = new WeakMap<object, number>()
|
|
39
|
+
const symbolIds = new Map<symbol, number>()
|
|
40
|
+
let nextIdentityId = 1
|
|
41
|
+
|
|
42
|
+
function identityId(value: object | symbol): number {
|
|
43
|
+
if (typeof value === 'symbol') {
|
|
44
|
+
const current = symbolIds.get(value)
|
|
45
|
+
if (current !== undefined) return current
|
|
46
|
+
const created = nextIdentityId++
|
|
47
|
+
symbolIds.set(value, created)
|
|
48
|
+
return created
|
|
49
|
+
}
|
|
50
|
+
const current = functionIds.get(value)
|
|
51
|
+
if (current !== undefined) return current
|
|
52
|
+
const created = nextIdentityId++
|
|
53
|
+
functionIds.set(value, created)
|
|
54
|
+
return created
|
|
55
|
+
}
|
|
56
|
+
|
|
57
|
+
/** Equal Juno values always produce the same hash. */
|
|
58
|
+
export function structuralHash(value: unknown): string {
|
|
59
|
+
const seen = new Map<object, number>()
|
|
60
|
+
const visit = (item: unknown): string => {
|
|
61
|
+
if (item === null) return 'null'
|
|
62
|
+
switch (typeof item) {
|
|
63
|
+
case 'undefined': return 'undefined'
|
|
64
|
+
case 'boolean': return item ? 'boolean:1' : 'boolean:0'
|
|
65
|
+
case 'number': return Number.isNaN(item) ? 'number:NaN' : `number:${item === 0 ? 0 : item}`
|
|
66
|
+
case 'bigint': return `bigint:${item}`
|
|
67
|
+
case 'string': return `string:${JSON.stringify(item)}`
|
|
68
|
+
case 'symbol': return `symbol:${identityId(item)}`
|
|
69
|
+
case 'function': return `function:${identityId(item)}`
|
|
70
|
+
}
|
|
71
|
+
|
|
72
|
+
const object = item as Record<string, unknown>
|
|
73
|
+
const reference = seen.get(object)
|
|
74
|
+
if (reference !== undefined) return `ref:${reference}`
|
|
75
|
+
seen.set(object, seen.size)
|
|
76
|
+
const keys = Object.keys(object).filter(key =>
|
|
77
|
+
!(key === 'toString' && typeof object[key] === 'function' && (object[key] as { __junoDefaultToString?: boolean }).__junoDefaultToString)
|
|
78
|
+
).sort()
|
|
79
|
+
return `${Array.isArray(object) ? 'array' : 'object'}:{${keys.map(key => `${JSON.stringify(key)}:${visit(object[key])}`).join(',')}}`
|
|
80
|
+
}
|
|
81
|
+
return visit(value)
|
|
82
|
+
}
|
|
83
|
+
|
|
84
|
+
export function globalSet<T>(...initialItems: T[]): JunoSet<T> {
|
|
85
|
+
type Entry = { value: T; hash: string; active: boolean }
|
|
86
|
+
const order: Entry[] = []
|
|
87
|
+
const buckets = new Map<string, Set<Entry>>()
|
|
88
|
+
const identities = new WeakMap<object, Entry>()
|
|
89
|
+
let activeLength = 0
|
|
90
|
+
|
|
91
|
+
const addToBucket = (entry: Entry): void => {
|
|
92
|
+
let bucket = buckets.get(entry.hash)
|
|
93
|
+
if (!bucket) buckets.set(entry.hash, bucket = new Set<Entry>())
|
|
94
|
+
bucket.add(entry)
|
|
95
|
+
}
|
|
96
|
+
const removeFromBucket = (entry: Entry): void => {
|
|
97
|
+
const bucket = buckets.get(entry.hash)
|
|
98
|
+
bucket?.delete(entry)
|
|
99
|
+
if (bucket?.size === 0) buckets.delete(entry.hash)
|
|
100
|
+
}
|
|
101
|
+
const refreshIdentity = (value: T): Entry | undefined => {
|
|
102
|
+
if ((typeof value !== 'object' || value === null) && typeof value !== 'function') return undefined
|
|
103
|
+
const entry = identities.get(value as object)
|
|
104
|
+
if (!entry?.active) return undefined
|
|
105
|
+
const hash = structuralHash(value)
|
|
106
|
+
if (hash !== entry.hash) {
|
|
107
|
+
removeFromBucket(entry)
|
|
108
|
+
entry.hash = hash
|
|
109
|
+
addToBucket(entry)
|
|
110
|
+
}
|
|
111
|
+
return entry
|
|
112
|
+
}
|
|
113
|
+
const find = (value: T): Entry | undefined => {
|
|
114
|
+
const identical = refreshIdentity(value)
|
|
115
|
+
if (identical) return identical
|
|
116
|
+
const bucket = buckets.get(structuralHash(value))
|
|
117
|
+
if (!bucket) return undefined
|
|
118
|
+
for (const entry of bucket) if (entry.active && junoEqual(entry.value, value)) return entry
|
|
119
|
+
return undefined
|
|
120
|
+
}
|
|
121
|
+
|
|
122
|
+
const result: JunoSet<T> = {
|
|
123
|
+
get length(): number { return activeLength },
|
|
124
|
+
includes(value: T): boolean { return find(value) !== undefined },
|
|
125
|
+
add(value: T): JunoSet<T> {
|
|
126
|
+
if (find(value)) return result
|
|
127
|
+
const entry: Entry = { value, hash: structuralHash(value), active: true }
|
|
128
|
+
order.push(entry)
|
|
129
|
+
addToBucket(entry)
|
|
130
|
+
if ((typeof value === 'object' && value !== null) || typeof value === 'function') identities.set(value as object, entry)
|
|
131
|
+
activeLength++
|
|
132
|
+
return result
|
|
133
|
+
},
|
|
134
|
+
delete(value: T): boolean {
|
|
135
|
+
const entry = find(value)
|
|
136
|
+
if (!entry) return false
|
|
137
|
+
entry.active = false
|
|
138
|
+
removeFromBucket(entry)
|
|
139
|
+
if ((typeof entry.value === 'object' && entry.value !== null) || typeof entry.value === 'function') identities.delete(entry.value as object)
|
|
140
|
+
activeLength--
|
|
141
|
+
return true
|
|
142
|
+
},
|
|
143
|
+
clear(): void {
|
|
144
|
+
for (const entry of order) entry.active = false
|
|
145
|
+
order.length = 0
|
|
146
|
+
buckets.clear()
|
|
147
|
+
activeLength = 0
|
|
148
|
+
},
|
|
149
|
+
iterator(): () => ({ value: T } | null) {
|
|
150
|
+
let index = 0
|
|
151
|
+
return () => {
|
|
152
|
+
while (index < order.length) {
|
|
153
|
+
const entry = order[index++]
|
|
154
|
+
if (entry.active) return { value: entry.value }
|
|
155
|
+
}
|
|
156
|
+
return null
|
|
157
|
+
}
|
|
158
|
+
}
|
|
159
|
+
}
|
|
160
|
+
for (const item of initialItems) result.add(item)
|
|
161
|
+
return result
|
|
162
|
+
}
|
|
163
|
+
|
|
164
|
+
export function immutableSnapshot<T>(value: T): T {
|
|
165
|
+
const clones = new WeakMap<object, any>()
|
|
166
|
+
const clone = (item: any): any => {
|
|
167
|
+
if (item === null || typeof item !== 'object' || item.__junoFunction || item.__junoConstructor) return item
|
|
168
|
+
const existing = clones.get(item)
|
|
169
|
+
if (existing) return existing
|
|
170
|
+
const copy: any = Array.isArray(item) ? [] : Object.create(Object.getPrototypeOf(item))
|
|
171
|
+
clones.set(item, copy)
|
|
172
|
+
for (const key of Reflect.ownKeys(item)) {
|
|
173
|
+
const descriptor = Object.getOwnPropertyDescriptor(item, key)
|
|
174
|
+
if (!descriptor) continue
|
|
175
|
+
if ('value' in descriptor) descriptor.value = clone(descriptor.value)
|
|
176
|
+
Object.defineProperty(copy, key, descriptor)
|
|
177
|
+
}
|
|
178
|
+
return copy
|
|
179
|
+
}
|
|
180
|
+
const seen = new WeakSet<object>()
|
|
181
|
+
const freeze = (item: any): any => {
|
|
182
|
+
if (item === null || typeof item !== 'object' || item.__junoFunction || item.__junoConstructor || seen.has(item)) return item
|
|
183
|
+
seen.add(item)
|
|
184
|
+
for (const key of Reflect.ownKeys(item)) {
|
|
185
|
+
const descriptor = Object.getOwnPropertyDescriptor(item, key)
|
|
186
|
+
if (descriptor && 'value' in descriptor) freeze(descriptor.value)
|
|
187
|
+
}
|
|
188
|
+
return Object.freeze(item)
|
|
189
|
+
}
|
|
190
|
+
return freeze(clone(value))
|
|
191
|
+
}
|
|
192
|
+
|
|
193
|
+
/** A structural set that stores immutable snapshots, keeping hashes stable. */
|
|
194
|
+
export function globalValueSet<T>(...initialItems: T[]): JunoSet<T> {
|
|
195
|
+
const storage = globalSet<T>()
|
|
196
|
+
const result: JunoSet<T> = {
|
|
197
|
+
get length(): number { return storage.length },
|
|
198
|
+
includes(value: T): boolean { return storage.includes(value) },
|
|
199
|
+
add(value: T): JunoSet<T> {
|
|
200
|
+
if (!storage.includes(value)) storage.add(immutableSnapshot(value))
|
|
201
|
+
return result
|
|
202
|
+
},
|
|
203
|
+
delete(value: T): boolean { return storage.delete(value) },
|
|
204
|
+
clear(): void { storage.clear() },
|
|
205
|
+
iterator(): () => ({ value: T } | null) { return storage.iterator() }
|
|
206
|
+
}
|
|
207
|
+
for (const item of initialItems) result.add(item)
|
|
208
|
+
return result
|
|
209
|
+
}
|
package/types/string.jn
ADDED
|
@@ -0,0 +1,33 @@
|
|
|
1
|
+
import { fromCharCode, fromCodePoint } from './string.ts'
|
|
2
|
+
|
|
3
|
+
type StringConstructor = {
|
|
4
|
+
fun (value: any): string
|
|
5
|
+
fromCharCode(...codes: number[]): string
|
|
6
|
+
fromCodePoint(...codePoints: number[]): string
|
|
7
|
+
raw(template: { raw: string[] }, ...substitutions: any[]): string
|
|
8
|
+
}
|
|
9
|
+
|
|
10
|
+
export const String : StringConstructor = {
|
|
11
|
+
fun (value : any) : string =
|
|
12
|
+
if value == null
|
|
13
|
+
return 'null'
|
|
14
|
+
return value!.toString()
|
|
15
|
+
|
|
16
|
+
fromCharCode,
|
|
17
|
+
|
|
18
|
+
fromCodePoint,
|
|
19
|
+
|
|
20
|
+
fun raw(template: { raw: string[] }, ...substitutions: any[]): string =
|
|
21
|
+
let result = ''
|
|
22
|
+
let index = 0
|
|
23
|
+
|
|
24
|
+
while index < template.raw.length
|
|
25
|
+
result += template.raw[index]
|
|
26
|
+
|
|
27
|
+
if index < substitutions.length
|
|
28
|
+
result += this(substitutions[index])
|
|
29
|
+
|
|
30
|
+
index++
|
|
31
|
+
|
|
32
|
+
return result
|
|
33
|
+
}
|
package/types/string.ts
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export const { fromCharCode, fromCodePoint } = globalThis.String
|
package/types/tuple.jn
ADDED
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
import { Iterable } from './iterable'
|
|
2
|
+
import { Range } from './range'
|
|
3
|
+
import { Array } from './array'
|
|
4
|
+
import { Items, Omit, Readonly } from 'juno:types'
|
|
5
|
+
|
|
6
|
+
type OmitFields = 'push' | 'pop' | 'shift' | 'unshift' | 'reverse' | 'sort' | 'fill' | 'slice' | 'splice' | 'copyWithin'
|
|
7
|
+
|
|
8
|
+
export constructor Tuple<T is any[]> : Readonly<T> & Omit<Array<T[number]>, OmitFields> {
|
|
9
|
+
fun (...items : T): Tuple<T> =
|
|
10
|
+
return Array.of(...items).omit(...Items<OmitFields>()).asReadonly()
|
|
11
|
+
}
|
package/README.md
DELETED