Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

✨ Support for grapheme on fc.string #5222

Merged
merged 26 commits into from
Aug 22, 2024
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
8 changes: 8 additions & 0 deletions .yarn/versions/2afbfedf.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"
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
import type { Arbitrary } from '../../check/arbitrary/definition/Arbitrary';
import { mapToConstant } from '../mapToConstant';
import type { GraphemeRange } from './data/GraphemeRanges';
import {
asciiAlphabetRanges,
autonomousDecomposableGraphemeRanges,
autonomousGraphemeRanges,
fullAlphabetRanges,
} from './data/GraphemeRanges';
import type { GraphemeRangeEntry } from './helpers/GraphemeRangesHelpers';
import { convertGraphemeRangeToMapToConstantEntry, intersectGraphemeRanges } from './helpers/GraphemeRangesHelpers';

/** @internal */
type StringUnitType = 'grapheme' | 'composite' | 'binary';
/** @internal */
type StringUnitAlphabet = 'full' | 'ascii';
/** @internal */
type StringUnitMapKey = `${StringUnitType}:${StringUnitAlphabet}`;

/**
* Caching all already instanciated variations of stringUnit
* @internal
*/
const registeredStringUnitInstancesMap: Partial<Record<StringUnitMapKey, Arbitrary<string>>> = Object.create(null);

/** @internal */
function getAlphabetRanges(alphabet: StringUnitAlphabet): GraphemeRange[] {
switch (alphabet) {
case 'full':
return fullAlphabetRanges;
case 'ascii':
return asciiAlphabetRanges;
}
}

/** @internal */
function getOrCreateStringUnitInstance(type: StringUnitType, alphabet: StringUnitAlphabet): Arbitrary<string> {
const key: StringUnitMapKey = `${type}:${alphabet}`;
const registered = registeredStringUnitInstancesMap[key];
if (registered !== undefined) {
return registered;
}
const alphabetRanges = getAlphabetRanges(alphabet);
const ranges = type === 'binary' ? alphabetRanges : intersectGraphemeRanges(alphabetRanges, autonomousGraphemeRanges);
const entries: GraphemeRangeEntry[] = [];
for (const range of ranges) {
entries.push(convertGraphemeRangeToMapToConstantEntry(range));
}
if (type === 'grapheme') {
const decomposedRanges = intersectGraphemeRanges(alphabetRanges, autonomousDecomposableGraphemeRanges);
for (const range of decomposedRanges) {
const rawEntry = convertGraphemeRangeToMapToConstantEntry(range);
entries.push({
num: rawEntry.num,
build: (idInGroup) => rawEntry.build(idInGroup).normalize('NFD'),
});
}
}
const stringUnitInstance = mapToConstant(...entries);
registeredStringUnitInstancesMap[key] = stringUnitInstance;
return stringUnitInstance;
}

/** @internal */
export function stringUnit(type: StringUnitType, alphabet: StringUnitAlphabet): Arbitrary<string> {
return getOrCreateStringUnitInstance(type, alphabet);
}
Loading
Loading