factory.ts
Advanced tools
Comparing version 0.3.0 to 0.3.1
@@ -1,31 +0,5 @@ | ||
export declare type FactoryFunc<T> = (item: Partial<T>) => T; | ||
export declare class Generator<T> { | ||
readonly func: (seq: number) => T; | ||
constructor(func: (seq: number) => T); | ||
build(seq: number): T; | ||
} | ||
export declare class Derived<TOwner, TProperty> { | ||
readonly func: (owner: TOwner, seq: number) => TProperty; | ||
constructor(func: (owner: TOwner, seq: number) => TProperty); | ||
build(owner: TOwner, seq: number): TProperty; | ||
} | ||
export declare class Factory<T> { | ||
readonly builder: Builder<T>; | ||
private seqNum; | ||
constructor(builder: Builder<T>); | ||
build(item?: Partial<T>): T; | ||
buildList(count: number, item?: Partial<T>): T[]; | ||
extend(def: Partial<Builder<T>>): Factory<T>; | ||
withDerivation<KOut extends keyof T>(kOut: KOut, f: (v1: T, seq: number) => T[KOut]): Factory<T>; | ||
withDerivation1<K1 extends keyof T, KOut extends keyof T>(kInput: [K1], kOut: KOut, f: (v1: T[K1], seq: number) => T[KOut]): Factory<T>; | ||
withDerivation2<K1 extends keyof T, K2 extends keyof T, KOut extends keyof T>(kInput: [K1, K2], kOut: KOut, f: (v1: T[K1], v2: T[K2], seq: number) => T[KOut]): Factory<T>; | ||
withDerivation3<K1 extends keyof T, K2 extends keyof T, K3 extends keyof T, KOut extends keyof T>(kInput: [K1, K2, K3], kOut: KOut, f: (v1: T[K1], v2: T[K2], v3: T[K3], seq: number) => T[KOut]): Factory<T>; | ||
withDerivation4<K1 extends keyof T, K2 extends keyof T, K3 extends keyof T, K4 extends keyof T, KOut extends keyof T>(kInput: [K1, K2, K3, K4], kOut: KOut, f: (v1: T[K1], v2: T[K2], v3: T[K3], v4: T[K4], seq: number) => T[KOut]): Factory<T>; | ||
withDerivation5<K1 extends keyof T, K2 extends keyof T, K3 extends keyof T, K4 extends keyof T, K5 extends keyof T, KOut extends keyof T>(kInput: [K1, K2, K3, K4, K5], kOut: KOut, f: (v1: T[K1], v2: T[K2], v3: T[K3], v4: T[K4], v5: T[K5], seq: number) => T[KOut]): Factory<T>; | ||
} | ||
export declare type Builder<T> = { | ||
[P in keyof T]: T[P] | Generator<T[P]> | Derived<T, T[P]>; | ||
}; | ||
export declare function val<T>(val: T): Generator<T>; | ||
export declare function each<T>(f: (seqNum: number) => T): Generator<T>; | ||
export declare function makeFactory<T>(builder: Builder<T>): Factory<T>; | ||
import * as Async from "./async"; | ||
export { Async }; | ||
import * as Sync from "./async"; | ||
export { Sync }; | ||
export { RecPartial, FactoryFunc, Generator, Derived, Factory, Builder, val, each, makeFactory } from "./sync"; |
125
lib/index.js
"use strict"; | ||
class Generator { | ||
constructor(func) { | ||
this.func = func; | ||
} | ||
build(seq) { | ||
return this.func(seq); | ||
} | ||
} | ||
exports.Generator = Generator; | ||
class Derived { | ||
constructor(func) { | ||
this.func = func; | ||
} | ||
build(owner, seq) { | ||
const ret = this.func(owner, seq); | ||
return ret; | ||
} | ||
} | ||
exports.Derived = Derived; | ||
class Factory { | ||
constructor(builder) { | ||
this.builder = builder; | ||
this.seqNum = 0; | ||
} | ||
build(item) { | ||
this.seqNum++; | ||
const base = buildBase(this.seqNum, this.builder); | ||
const v = Object.assign({}, base.value, item); | ||
if (item) { | ||
const keys = Object.keys(item); | ||
for (const der of base.derived) { | ||
if (keys.indexOf(der.key) < 0) { | ||
v[der.key] = der.derived.build(v, this.seqNum); | ||
} | ||
} | ||
} | ||
return v; | ||
} | ||
buildList(count, item) { | ||
const ts = Array(count); // allocate to correct size | ||
for (let i = 0; i < count; i++) { | ||
ts[i] = this.build(item); | ||
} | ||
return ts; | ||
} | ||
extend(def) { | ||
const builder = Object.assign({}, this.builder, def); | ||
return new Factory(builder); | ||
} | ||
withDerivation(kOut, f) { | ||
const partial = {}; | ||
partial[kOut] = new Derived(f); | ||
return this.extend(partial); | ||
} | ||
withDerivation1(kInput, kOut, f) { | ||
const partial = {}; | ||
partial[kOut] = new Derived((t, i) => f(t[kInput[0]], i)); | ||
return this.extend(partial); | ||
} | ||
withDerivation2(kInput, kOut, f) { | ||
const partial = {}; | ||
partial[kOut] = new Derived((t, i) => f(t[kInput[0]], t[kInput[1]], i)); | ||
return this.extend(partial); | ||
} | ||
withDerivation3(kInput, kOut, f) { | ||
const partial = {}; | ||
partial[kOut] = new Derived((t, i) => f(t[kInput[0]], t[kInput[1]], t[kInput[2]], i)); | ||
return this.extend(partial); | ||
} | ||
withDerivation4(kInput, kOut, f) { | ||
const partial = {}; | ||
partial[kOut] = new Derived((t, i) => f(t[kInput[0]], t[kInput[1]], t[kInput[2]], t[kInput[3]], i)); | ||
return this.extend(partial); | ||
} | ||
withDerivation5(kInput, kOut, f) { | ||
const partial = {}; | ||
partial[kOut] = new Derived((t, i) => f(t[kInput[0]], t[kInput[1]], t[kInput[2]], t[kInput[3]], t[kInput[4]], i)); | ||
return this.extend(partial); | ||
} | ||
} | ||
exports.Factory = Factory; | ||
function val(val) { | ||
return new Generator(() => val); | ||
} | ||
exports.val = val; | ||
function each(f) { | ||
return new Generator(f); | ||
} | ||
exports.each = each; | ||
function buildBase(seqNum, builder) { | ||
const t = {}; | ||
const keys = Object.getOwnPropertyNames(builder); | ||
const derived = []; | ||
for (const key of keys) { | ||
const v = builder[key]; | ||
let value = v; | ||
if (!!v && typeof v === "object") { | ||
if (v.constructor === Generator) { | ||
value = v.build(seqNum); | ||
} | ||
else if (v.constructor == Derived) { | ||
derived.push({ key, derived: v }); | ||
} | ||
} | ||
t[key] = value; | ||
} | ||
return { value: t, derived }; | ||
} | ||
function makeFactory(builder) { | ||
return new Factory(builder); | ||
} | ||
exports.makeFactory = makeFactory; | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
const Async = require("./async"); | ||
exports.Async = Async; | ||
const Sync = require("./async"); | ||
exports.Sync = Sync; | ||
// for now, for backwards compat | ||
var sync_1 = require("./sync"); | ||
exports.Generator = sync_1.Generator; | ||
exports.Derived = sync_1.Derived; | ||
exports.Factory = sync_1.Factory; | ||
exports.val = sync_1.val; | ||
exports.each = sync_1.each; | ||
exports.makeFactory = sync_1.makeFactory; | ||
//# sourceMappingURL=index.js.map |
{ | ||
"name": "factory.ts", | ||
"version": "0.3.0", | ||
"version": "0.3.1", | ||
"license": "MIT", | ||
"description": "A Typescript test data factory similar to factory_bot and rosie", | ||
"scripts": { | ||
"build": "tsc -p .", | ||
"test": "NODE_ENV=test mocha --require spec/setup.js --require ts-node/register", | ||
@@ -16,3 +17,5 @@ "testAll": "NODE_ENV=test mocha --require spec/setup.js --require ts-node/register 'spec/**/*.spec.ts*'" | ||
"typings": "lib/index", | ||
"files": ["lib/"], | ||
"files": [ | ||
"lib/" | ||
], | ||
"author": "Will Pleasant-Ryan", | ||
@@ -38,2 +41,2 @@ "devDependencies": { | ||
} | ||
} | ||
} |
@@ -7,3 +7,3 @@ # factory.ts | ||
Version 0.3.0 introduces a new set of async factory methods for cases where asynchronicity is required to generate values. | ||
Version 0.3.1 introduces a new set of async factory methods for cases where asynchronicity is required to generate values. | ||
@@ -10,0 +10,0 @@ ## Example |
Sorry, the diff of this file is not supported yet
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
Major refactor
Supply chain riskPackage has recently undergone a major refactor. It may be unstable or indicate significant internal changes. Use caution when updating to versions that include significant changes.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
36728
12
433
1