Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions .changeset/short-nails-travel.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@traversable/json": patch
---

fix(json): removes `Arbitrary` module so users don't have to download fast-check as a dep
110 changes: 55 additions & 55 deletions packages/json/src/arbitrary.ts
Original file line number Diff line number Diff line change
@@ -1,61 +1,61 @@
import type { Json } from './exports.js'
import * as fc from 'fast-check'
// import type { Json } from './exports.js'
// import * as fc from 'fast-check'

/** @internal */
const depthIdentifier = fc.createDepthIdentifier()
// /** @internal */
// const depthIdentifier = fc.createDepthIdentifier()

/** @internal */
const identifier = fc.stringMatching(/[_$a-zA-Z][_$a-zA-Z0-9]+/g)
// /** @internal */
// const identifier = fc.stringMatching(/[_$a-zA-Z][_$a-zA-Z0-9]+/g)

/** @internal */
const scalars = [
fc.constant(null),
fc.constant(undefined),
fc.boolean(),
fc.integer(),
fc.float(),
fc.string(),
]
// /** @internal */
// const scalars = [
// fc.constant(null),
// fc.constant(undefined),
// fc.boolean(),
// fc.integer(),
// fc.float(),
// fc.string(),
// ]

/** @internal */
const seed: fc.LetrecTypedBuilder<Seed> = (LOOP) => {
return {
scalar: fc.oneof(...scalars),
array: fc.array(LOOP('any')),
object: fc.dictionary(identifier, LOOP('any')),
any: fc.oneof(
{ depthIdentifier, maxDepth: 2, depthSize: 'xsmall', withCrossShrink: true },
...scalars,
LOOP('scalar'),
LOOP('array'),
LOOP('object'),
)
}
}
// /** @internal */
// const seed: fc.LetrecTypedBuilder<Seed> = (LOOP) => {
// return {
// scalar: fc.oneof(...scalars),
// array: fc.array(LOOP('any')),
// object: fc.dictionary(identifier, LOOP('any')),
// any: fc.oneof(
// { depthIdentifier, maxDepth: 2, depthSize: 'xsmall', withCrossShrink: true },
// ...scalars,
// LOOP('scalar'),
// LOOP('array'),
// LOOP('object'),
// )
// }
// }

/**
* ## {@link Seed `Json.Seed`}
*
* This type describes an object whose values are [fast-check](https://github.com/dubzzz/fast-check)
* arbitraries that are capable of generating arbitrary, valid JSON values.
*
* See also:
* - {@link Arbitrary `Json.Arbitrary`}
*/
export interface Seed {
any: Json
scalar: Json.Scalar
array: readonly this['any'][]
object: Record<string, this['any']>
}
// /**
// * ## {@link Seed `Json.Seed`}
// *
// * This type describes an object whose values are [fast-check](https://github.com/dubzzz/fast-check)
// * arbitraries that are capable of generating arbitrary, valid JSON values.
// *
// * See also:
// * - {@link Arbitrary `Json.Arbitrary`}
// */
// export interface Seed {
// any: Json
// scalar: Json.Scalar
// array: readonly this['any'][]
// object: Record<string, this['any']>
// }

/**
* ## {@link Arbitrary `Json.Arbitrary`}
*
* An object whose values are [fast-check](https://github.com/dubzzz/fast-check)
* arbitraries that are capable of generating arbitrary, valid JSON values.
*
* See also:
* - {@link Seed `Json.Seed`}
*/
export const Arbitrary = fc.letrec(seed)
// /**
// * ## {@link Arbitrary `Json.Arbitrary`}
// *
// * An object whose values are [fast-check](https://github.com/dubzzz/fast-check)
// * arbitraries that are capable of generating arbitrary, valid JSON values.
// *
// * See also:
// * - {@link Seed `Json.Seed`}
// */
// export const Arbitrary = fc.letrec(seed)
1 change: 0 additions & 1 deletion packages/json/src/exports.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import * as _ from './functor.js'
import { Arbitrary as _Arbitrary } from './arbitrary.js'

export { VERSION } from './version.js'
export { Cache } from './cache.js'
Expand Down
2 changes: 0 additions & 2 deletions packages/json/src/json.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
export { Arbitrary } from './arbitrary.js'

export type {
Fixpoint,
Free,
Expand Down
61 changes: 61 additions & 0 deletions packages/json/test/arbitrary.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
import { Json } from '@traversable/json'
import * as fc from 'fast-check'

/** @internal */
const depthIdentifier = fc.createDepthIdentifier()

/** @internal */
const identifier = fc.stringMatching(/[_$a-zA-Z][_$a-zA-Z0-9]+/g)

/** @internal */
const scalars = [
fc.constant(null),
fc.constant(undefined),
fc.boolean(),
fc.integer(),
fc.float(),
fc.string(),
]

/** @internal */
const seed: fc.LetrecTypedBuilder<Seed> = (LOOP) => {
return {
scalar: fc.oneof(...scalars),
array: fc.array(LOOP('any')),
object: fc.dictionary(identifier, LOOP('any')),
any: fc.oneof(
{ depthIdentifier, maxDepth: 2, depthSize: 'xsmall', withCrossShrink: true },
...scalars,
LOOP('scalar'),
LOOP('array'),
LOOP('object'),
)
}
}

/**
* ## {@link Seed `Json.Seed`}
*
* This type describes an object whose values are [fast-check](https://github.com/dubzzz/fast-check)
* arbitraries that are capable of generating arbitrary, valid JSON values.
*
* See also:
* - {@link Arbitrary `Json.Arbitrary`}
*/
export interface Seed {
any: Json
scalar: Json.Scalar
array: readonly this['any'][]
object: Record<string, this['any']>
}

/**
* ## {@link Arbitrary `Json.Arbitrary`}
*
* An object whose values are [fast-check](https://github.com/dubzzz/fast-check)
* arbitraries that are capable of generating arbitrary, valid JSON values.
*
* See also:
* - {@link Seed `Json.Seed`}
*/
export const Arbitrary = fc.letrec(seed)
4 changes: 3 additions & 1 deletion packages/json/test/cache.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@ import { fc, test } from '@fast-check/vitest'
import { Cache, Json } from '@traversable/json'
import { symbol } from '@traversable/registry'

import { Arbitrary } from './arbitrary.js'

vi.describe('〖⛳️〗‹‹‹ ❲@traversable/json❳', () => {
const createBadCache = Cache.new as any
const index = { depth: 0, path: [] }
Expand All @@ -20,7 +22,7 @@ vi.describe('〖⛳️〗‹‹‹ ❲@traversable/json❳', () => {
})

test.prop(
[fc.dictionary(fc.string(), Json.Arbitrary.object)], {
[fc.dictionary(fc.string(), Arbitrary.object)], {
// numRuns: 10_000,
examples: [
[{ "toString": {} }]
Expand Down
11 changes: 7 additions & 4 deletions packages/json/test/json.test.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
import { Json } from '@traversable/json'
import * as vi from 'vitest'
import { test } from '@fast-check/vitest'
import * as fc from 'fast-check'
import { Arbitrary } from './arbitrary.js'


const addMetadata = (data: Json) =>
!Json.isObject(data) ? data
Expand All @@ -16,17 +19,17 @@ const rmMetadata = (data: Json) => {

vi.describe('〖⛳️〗‹‹‹ ❲@traversable/json❳', () => {
test.prop(
[Json.Arbitrary.any], {
[Arbitrary.any], {
// numRuns: 50_000
})(
'〖⛳️〗› ❲Json#Functor❳: Functor.map preserves structure',
(json) => {
vi.assert.deepEqual(Json.Functor.map((x) => x)(json), json)
}
);
)

test.prop(
[Json.Arbitrary.object], {
[Arbitrary.object], {
// numRuns: 50_000,
})(
'〖⛳️〗› ❲Json#fold + Json#unfold❳',
Expand All @@ -38,5 +41,5 @@ vi.describe('〖⛳️〗‹‹‹ ❲@traversable/json❳', () => {
vi.assert.notDeepEqual(withoutMetadata, withMetadata)
vi.assert.deepEqual(json, withoutMetadata)
}
);
)
})