Skip to content

Commit

Permalink
✨ Add support for depthIdentifier to dictionary (#4548)
Browse files Browse the repository at this point in the history
* ✨ Add support for `depthIdentifier` to `dictionary`

* minor'
  • Loading branch information
dubzzz authored Dec 16, 2023
1 parent fc30c3c commit 3b0560d
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 1 deletion.
8 changes: 8 additions & 0 deletions .yarn/versions/7dbac1db.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
releases:
fast-check: minor

declined:
- "@fast-check/ava"
- "@fast-check/jest"
- "@fast-check/vitest"
- "@fast-check/worker"
11 changes: 11 additions & 0 deletions packages/fast-check/src/arbitrary/dictionary.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import type { SizeForArbitrary } from './_internals/helpers/MaxLengthFromMinLeng
import { keyValuePairsToObjectMapper, keyValuePairsToObjectUnmapper } from './_internals/mappers/KeyValuePairsToObject';
import { constant } from './constant';
import { boolean } from './boolean';
import type { DepthIdentifier } from './_internals/helpers/DepthContext';

/** @internal */
function dictionaryKeyExtractor(entry: [string, unknown]): string {
Expand Down Expand Up @@ -34,6 +35,15 @@ export interface DictionaryConstraints {
* @remarks Since 2.22.0
*/
size?: SizeForArbitrary;
/**
* Depth identifier can be used to share the current depth between several instances.
*
* By default, if not specified, each instance of dictionary will have its own depth.
* In other words: you can have depth=1 in one while you have depth=100 in another one.
*
* @remarks Since 3.15.0
*/
depthIdentifier?: DepthIdentifier | string;
/**
* Do not generate objects with null prototype
* @defaultValue true
Expand Down Expand Up @@ -63,6 +73,7 @@ export function dictionary<T>(
maxLength: constraints.maxKeys,
size: constraints.size,
selector: dictionaryKeyExtractor,
depthIdentifier: constraints.depthIdentifier,
}),
noNullPrototype ? constant(false) : boolean(),
).map(keyValuePairsToObjectMapper, keyValuePairsToObjectUnmapper);
Expand Down
3 changes: 2 additions & 1 deletion website/docs/core-blocks/arbitraries/composites/object.md
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ Generate dictionaries containing keys generated using `keyArb` and values genera
**Signatures:**

- `fc.dictionary(keyArb, valueArb)`
- `fc.dictionary(keyArb, valueArb, {minKeys?, maxKeys?, size?, noNullPrototype?})`
- `fc.dictionary(keyArb, valueArb, {minKeys?, maxKeys?, size?, noNullPrototype?, depthIdentifier?})`

**with:**

Expand All @@ -23,6 +23,7 @@ Generate dictionaries containing keys generated using `keyArb` and values genera
- `maxKeys?` — default: `0x7fffffff` [more](/docs/configuration/larger-entries-by-default/#size-explained)_maximal number of keys in the generated instances (included)_
- `size?` — default: `undefined` [more](/docs/configuration/larger-entries-by-default/#size-explained)_how large should the generated values be?_
- `noNullPrototype?` — default: `true`_only generate objects based on the Object-prototype, do not generate any object with null-prototype_
- `depthIdentifier?` — default: `undefined`_share the depth between instances using the same `depthIdentifier`_

**Usages:**

Expand Down

0 comments on commit 3b0560d

Please sign in to comment.