Skip to content

Commit

Permalink
feat(shape): Support generating objects of fake data
Browse files Browse the repository at this point in the history
  • Loading branch information
justinvdm committed May 7, 2020
1 parent b64ec17 commit fc13ec1
Show file tree
Hide file tree
Showing 8 changed files with 315 additions and 1 deletion.
19 changes: 19 additions & 0 deletions index.d.ts
Expand Up @@ -203,9 +203,28 @@ declare const tuple: Tuple

export { tuple }

export interface Shape {
<Makers extends { [s: string]: Maker<unknown> }>(makers: Makers): (
input: Input
) => ShapeReturnType<Makers>
<Makers extends { [s: string]: Maker<unknown> }>(
input: Input,
makers: Makers
): ShapeReturnType<Makers>
}

declare const shape: Shape

export { shape }

type MakerResult<M> = M extends Maker<infer R> ? R : never

type WeightedMakerResult<M> = M extends WeightedMaker<infer R> ? R : never

type ShapeReturnType<Makers extends { [s: string]: Maker<unknown> }> = {
[K in keyof Makers]: MakerResult<Makers[K]>
}

export type TupleReturnType<Makers extends AnyMakers> = Makers extends Makers1<
infer V1
>
Expand Down
1 change: 1 addition & 0 deletions index.js
Expand Up @@ -3,6 +3,7 @@ exports.hash = require('./hash')
exports.oneOf = require('./oneOf')
exports.someOf = require('./someOf')
exports.tuple = require('./tuple')
exports.shape = require('./shape')
exports.times = require('./times')
exports.join = require('./join')
exports.oneOfWeighted = require('./oneOfWeighted')
Expand Down
1 change: 1 addition & 0 deletions index.mjs
Expand Up @@ -2,6 +2,7 @@ export { default as hash } from './has'

export { default as someOf } from './someOf'
export { default as tuple } from './tuple'
export { default as shape } from './shape'
export { default as times } from './times'
export { default as join } from './join'
export { default as oneOf } from './oneOf'
Expand Down
32 changes: 32 additions & 0 deletions index.test-d.ts
Expand Up @@ -7,6 +7,7 @@ import {
float,
word,
tuple,
shape,
someOf,
times,
join,
Expand All @@ -26,6 +27,15 @@ expectType<number | string>(
)
expectType<(number | string)[]>(someOf(null, [2, 3], [int, word]))
expectType<number[]>(times(null, [2, 3], int))
expectType<{
a: number
b: string
}>(
shape(null, {
a: int,
b: word
})
)

// ## constant items
expectType<[number, string]>(tuple(null, [2, '!']))
Expand All @@ -39,6 +49,15 @@ expectType<number | string>(
)
expectType<(number | string)[]>(someOf(null, [3, 3], [2, '!']))
expectType<number[]>(times(null, [2, 3], 23))
expectType<{
a: number
b: string
}>(
shape(null, {
a: 2,
b: '!'
})
)

// ## currying
expectType<(input: Input) => [number]>(tuple([int]))
Expand All @@ -52,6 +71,19 @@ expectType<(input: Input) => number>(
)
expectType<(input: Input) => string[]>(someOf([3, 3], ['a', 'b']))
expectType<(input: Input) => string[]>(times([2, 3], word))
expectType<
(
input: Input
) => {
a: number
b: string
}
>(
shape({
a: int,
b: word
})
)

// ## join: joiner types
declare function numJoiner(v: (number | string)[]): number
Expand Down
29 changes: 29 additions & 0 deletions shape.js
@@ -0,0 +1,29 @@
var hash = require('./hash')
var resolve = require('./utils/resolve')

var hasOwnProperty = Object.prototype.hasOwnProperty

function shape(a, b) {
return b != null ? shapeMain(a, b) : shapeCurried(a)
}

function shapeMain(input, properties) {
var id = hash(input)
var results = {}

for (var k in properties) {
if (hasOwnProperty.call(properties, k)) {
results[k] = resolve(hash([id, 'shape', k]), properties[k])
}
}

return results
}

function shapeCurried(properties) {
return function shapeCurriedFn(input) {
return shapeMain(input, properties)
}
}

module.exports = shape

0 comments on commit fc13ec1

Please sign in to comment.