refactor: Switch to 'map-like object keyed by string and type vector' format
See https://code.studioinfinity.org/glen/nanomath/wiki/Item-Specifications. Also stubs out the TypeDispatcher, mocking the merge function, so we can see that all of the proper things will be added. Ready for initial implementation of the TypeDispatcher.
This commit is contained in:
parent
040ec377a1
commit
69ef928b6e
10 changed files with 84 additions and 15 deletions
5
src/core/Type.js
Normal file
5
src/core/Type.js
Normal file
|
@ -0,0 +1,5 @@
|
|||
export class Type {
|
||||
constructor(f) {
|
||||
this.test = f
|
||||
}
|
||||
}
|
37
src/core/TypeDispatcher.js
Normal file
37
src/core/TypeDispatcher.js
Normal file
|
@ -0,0 +1,37 @@
|
|||
import {Type} from './Type.js'
|
||||
import {Implementations, isPlainObject} from './helpers.js'
|
||||
|
||||
export class TypeDispatcher {
|
||||
constructor(...specs) {
|
||||
for (const spec of specs) this.merge(spec)
|
||||
}
|
||||
|
||||
merge(spec) {
|
||||
if (!spec) return
|
||||
if (typeof spec != 'object') {
|
||||
throw new TypeError(
|
||||
`TypeDispatcher specifications must be objects, not '${spec}'.`)
|
||||
}
|
||||
for (const key in spec) {
|
||||
const val = spec[key]
|
||||
if (val instanceof Type) {
|
||||
console.log(`Pretending to install type ${key}: ${val}`)
|
||||
continue
|
||||
}
|
||||
if (val instanceof Implementations) {
|
||||
console.log(`Pretending to install implementations for ${key}`)
|
||||
console.log(` --> ${val}`)
|
||||
continue
|
||||
}
|
||||
if (typeof val === 'function') {
|
||||
console.log(`Pretend install of catchall implementation for ${key}`)
|
||||
continue
|
||||
}
|
||||
if (isPlainObject(val)) {
|
||||
this.merge(val)
|
||||
continue
|
||||
}
|
||||
console.log(`Pretend install of catchall value for ${key}: ${val}`)
|
||||
}
|
||||
}
|
||||
}
|
27
src/core/helpers.js
Normal file
27
src/core/helpers.js
Normal file
|
@ -0,0 +1,27 @@
|
|||
import {Type} from './Type.js'
|
||||
|
||||
export class Implementations {
|
||||
constructor(imps) {
|
||||
this.patterns = new Map()
|
||||
for (let i = 0; i < imps.length; ++i) {
|
||||
let pattern = imps[i++]
|
||||
if (!Array.isArray(pattern)) pattern = [pattern]
|
||||
if (!pattern.every(item => item instanceof Type)) {
|
||||
throw new TypeError(
|
||||
`Implementation pattern ${pattern} contains non-Type entry`)
|
||||
}
|
||||
this.patterns.set(pattern, imps[i])
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
export const onType = (...imps) => new Implementations(imps)
|
||||
|
||||
export const Returns = (type, f) => (f.returns = type, f)
|
||||
|
||||
export const isPlainObject = (obj) => {
|
||||
if (typeof obj !== 'object') return false
|
||||
if (!obj) return false // excludes null
|
||||
const proto = Object.getPrototypeOf(obj)
|
||||
return !proto || proto === Object.prototype
|
||||
}
|
Loading…
Add table
Add a link
Reference in a new issue