UNPKG

concordance-comparator

Version:

- `npm install` - install dependencies - `npm test` - run tests - `npm run compile` - compile your code into the `lib/` folder

96 lines 3.13 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); const formatUtils_1 = require("concordance/lib/formatUtils"); const lineBuilder_1 = require("concordance/lib/lineBuilder"); function elementFactory(api, cls, clsFactory) { function describe(props) { return new DescribedPromiseValue(props); } function deserialize(state, recursor) { return new DeserializedPromiseValue(state, recursor); } const tag = Symbol(`@concordance/cls/${cls.name}`); class PromiseValue extends api.ObjectValue { constructor(props) { super(props); } compare(expected) { if (!(this.value instanceof cls)) return api.UNEQUAL; if (!(expected.value instanceof cls)) return api.UNEQUAL; return this.value.compare(expected.value) ? api.DEEP_EQUAL : api.UNEQUAL; } formatShallow(theme, indent) { const value = this.value; return new class extends formatUtils_1.ObjectFormatter { constructor(object, theme, indent) { super(object, theme, indent); } finalize() { return lineBuilder_1.single(value.toString()); } }(this, theme, indent); } serialize() { return [this.value.serialize(), super.serialize()]; } } Object.defineProperty(PromiseValue.prototype, 'isComplex', { value: true }); Object.defineProperty(PromiseValue.prototype, 'tag', { value: tag }); class DescribedPromiseValue extends api.DescribedMixin(PromiseValue) { constructor(props) { super(props); } compare(expected) { return super.compare(expected); } } class DeserializedPromiseValue extends api.DeserializedMixin(PromiseValue) { constructor(state, recursor) { super(state[1], recursor); if (clsFactory) this.value = clsFactory(state[0]); else { this.value = new cls(); this.value.deserialize(state[0]); } } compare(expected) { return super.compare(expected); } } return { describe, deserialize, tag, }; } let index = 0; function factory(cls, clsFactory = undefined) { index++; const name = `@concordance/cls/${cls.name}`; const apiVersion = 1; const minimalConcordanceVersion = '1.0.0'; const serializerVersion = 2; const theme = {}; function register(api) { const element = elementFactory(api, cls, clsFactory); api.addDescriptor(index, element.tag, element.deserialize); return (value) => { if (value instanceof cls) return element.describe; return null; }; } return { name, apiVersion, minimalConcordanceVersion, serializerVersion, theme, register, }; } exports.factory = factory; //# sourceMappingURL=index.js.map