@arktype/util
Advanced tools
Comparing version 0.0.9 to 0.0.10
@@ -30,25 +30,2 @@ import { attest } from "@arktype/attest" | ||
class StringChecker extends compose(Describable, Boundable<string>) { | ||
sizeOf(data: string) { | ||
return data.length | ||
} | ||
writeDefaultDescription() { | ||
return "foo" | ||
} | ||
} | ||
const shortString = new StringChecker( | ||
{ limit: 5 }, | ||
{ description: "a short string" } | ||
) | ||
attest(shortString.check("foo")).equals(true) | ||
attest(shortString.check("toolong")).equals(false) | ||
attest(shortString.description).equals("a short string") | ||
attest(shortString.writeDefaultDescription()).equals("foo") | ||
attest([ | ||
shortString instanceof StringChecker, | ||
shortString instanceof Boundable, | ||
shortString instanceof Describable | ||
]).equals([true, true, true]) | ||
describe("traits", () => { | ||
@@ -55,0 +32,0 @@ it("compose", () => { |
import type { intersectParameters } from "./intersections.js"; | ||
import type { NonEmptyList } from "./lists.js"; | ||
import type { Constructor } from "./objectKinds.js"; | ||
export type TraitComposition = <traits extends NonEmptyList<Constructor>>(...traits: traits) => compose<traits>; | ||
export type TraitComposition = <traits extends readonly Constructor[]>(...traits: traits) => compose<traits>; | ||
export declare const traitsOf: unique symbol; | ||
@@ -6,0 +5,0 @@ export declare abstract class Trait { |
@@ -7,2 +7,12 @@ export const traitsOf = Symbol("hasTraits"); | ||
} | ||
const collectPrototypeDescriptors = (trait) => { | ||
let proto = trait.prototype; | ||
let result = {}; | ||
do { | ||
// ensure prototypes are sorted from lowest to highest precedence | ||
result = Object.assign(Object.getOwnPropertyDescriptors(proto), result); | ||
proto = Object.getPrototypeOf(proto); | ||
} while (proto !== Object.prototype && proto !== null); | ||
return result; | ||
}; | ||
export const compose = ((...traits) => { | ||
@@ -16,3 +26,3 @@ const base = function (...args) { | ||
for (const trait of traits) { | ||
base.prototype = Object.create(trait.prototype, Object.getOwnPropertyDescriptors(base.prototype)); | ||
Object.defineProperties(base.prototype, collectPrototypeDescriptors(trait)); | ||
} | ||
@@ -19,0 +29,0 @@ return base; |
{ | ||
"name": "@arktype/util", | ||
"version": "0.0.9", | ||
"version": "0.0.10", | ||
"author": { | ||
@@ -5,0 +5,0 @@ "name": "David Blass", |
import type { intersectParameters } from "./intersections.js" | ||
import type { NonEmptyList } from "./lists.js" | ||
import type { Constructor } from "./objectKinds.js" | ||
export type TraitComposition = <traits extends NonEmptyList<Constructor>>( | ||
export type TraitComposition = <traits extends readonly Constructor[]>( | ||
...traits: traits | ||
@@ -17,2 +16,14 @@ ) => compose<traits> | ||
const collectPrototypeDescriptors = (trait: Function) => { | ||
let proto = trait.prototype | ||
let result: PropertyDescriptorMap = {} | ||
do { | ||
// ensure prototypes are sorted from lowest to highest precedence | ||
result = Object.assign(Object.getOwnPropertyDescriptors(proto), result) | ||
proto = Object.getPrototypeOf(proto) | ||
} while (proto !== Object.prototype && proto !== null) | ||
return result | ||
} | ||
export const compose = ((...traits: Function[]) => { | ||
@@ -25,7 +36,5 @@ const base: any = function (this: any, ...args: any[]) { | ||
} | ||
for (const trait of traits) { | ||
base.prototype = Object.create( | ||
trait.prototype, | ||
Object.getOwnPropertyDescriptors(base.prototype) | ||
) | ||
Object.defineProperties(base.prototype, collectPrototypeDescriptors(trait)) | ||
} | ||
@@ -32,0 +41,0 @@ return base |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
176684
2498