-
Notifications
You must be signed in to change notification settings - Fork 9
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
9ccd780
commit 13b6471
Showing
5 changed files
with
250 additions
and
102 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -79,4 +79,4 @@ gen/ | |
# Other | ||
*.config.json | ||
.ignore.me.* | ||
/.vscode | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
{ | ||
// Use IntelliSense to learn about possible attributes. | ||
// Hover to view descriptions of existing attributes. | ||
// For more information, visit: https://go.microsoft.com/fwlink/?linkid=830387 | ||
"version": "0.2.0", | ||
"configurations": [ | ||
{ | ||
"type": "node", | ||
"request": "launch", | ||
"name": "Debug Test File", | ||
"autoAttachChildProcesses": true, | ||
"skipFiles": ["<node_internals>/**", "**/node_modules/**"], | ||
"program": "${workspaceRoot}/node_modules/vitest/vitest.mjs", | ||
"args": ["run", "${relativeFile}"], | ||
"smartStep": true, | ||
"console": "integratedTerminal" | ||
} | ||
] | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,29 +1,85 @@ | ||
import * as types from './typings' | ||
import { JexSet, jexEquals } from './jex-equals' | ||
|
||
const _primitiveExtends = <T extends types.JexPrimitive>( | ||
child: T | types.JexLiteral<T>, | ||
parent: types.JexType | ||
): boolean => { | ||
const isT = (x: types.JexType): x is T | types.JexLiteral<T> => x.type === child.type | ||
if (!isT(parent)) return false | ||
|
||
type Primitive = types.JexLiteral<T> | (T & { value: undefined }) | ||
const asPrimitive = (value: T | types.JexLiteral<T>): Primitive => | ||
'value' in value ? value : { ...value, value: undefined } | ||
|
||
const _child = asPrimitive(child) | ||
const _parent = asPrimitive(parent) | ||
|
||
if (_parent.value === undefined && _child.value === undefined) { | ||
return true | ||
} | ||
if (_parent.value === undefined) { | ||
return true | ||
} | ||
if (_parent.value === _child.value) { | ||
return true | ||
} | ||
return false | ||
} | ||
|
||
export const jexExtends = (child: types.JexType, parent: types.JexType): boolean => { | ||
if (parent.type === 'any') return true | ||
if (child.type === 'any') return true | ||
if (parent.type === 'any' || child.type === 'any') { | ||
return true | ||
} | ||
|
||
if (child.type === 'union') { | ||
return child.anyOf.every((c) => jexExtends(c, parent)) | ||
} | ||
|
||
const areEqual = jexEquals(child, parent) | ||
if (areEqual) return true | ||
if (parent.type === 'union') { | ||
return parent.anyOf.some((p) => jexExtends(child, p)) | ||
} | ||
|
||
if (child.type === 'object') { | ||
if (parent.type !== 'object') return false | ||
for (const [key, parentValue] of Object.entries(parent.properties)) { | ||
const childValue = child.properties[key] | ||
if (!childValue) return false | ||
if (!jexExtends(childValue, parentValue)) return false | ||
if (parent.type === 'map') { | ||
return Object.values(child.properties).every((c) => jexExtends(c, parent.items)) | ||
} | ||
return true | ||
if (parent.type === 'object') { | ||
return Object.entries(parent.properties).every(([key, parentValue]) => { | ||
const childValue = child.properties[key] | ||
if (childValue === undefined) return false | ||
return jexExtends(childValue, parentValue) | ||
}) | ||
} | ||
return false | ||
} | ||
|
||
if (child.type === 'union') { | ||
if (parent.type !== 'union') return false | ||
const childProps = new JexSet(child.anyOf) | ||
const parentProps = new JexSet(parent.anyOf) | ||
return parentProps.isSubsetOf(childProps) // parentProps ⊆ childProps | ||
if (child.type === 'map') { | ||
if (parent.type !== 'map') return false | ||
return jexExtends(child.items, parent.items) | ||
} | ||
|
||
return false | ||
if (child.type === 'tuple') { | ||
if (parent.type === 'array') { | ||
return child.items.every((c) => jexExtends(c, parent.items)) | ||
} | ||
if (parent.type === 'tuple') { | ||
const zipped = child.items.map( | ||
(c, i) => [c, parent.items[i]] satisfies [types.JexType, types.JexType | undefined] | ||
) | ||
return zipped.every(([c, p]) => p === undefined || jexExtends(c, p)) | ||
} | ||
return false | ||
} | ||
|
||
if (child.type === 'array') { | ||
if (parent.type !== 'array') return false | ||
return jexExtends(child.items, parent.items) | ||
} | ||
|
||
if (child.type === 'string' || child.type === 'number' || child.type === 'boolean') { | ||
return _primitiveExtends(child, parent) | ||
} | ||
|
||
return jexEquals(child, parent) | ||
} |
Oops, something went wrong.