@snaplet/copycat
Advanced tools
Comparing version 4.0.0 to 4.1.0
@@ -1,2 +0,2 @@ | ||
export declare const firstName: (input: import("fictional").JSONSerializable, options?: import("./oneOfString").OneOfOptions) => any; | ||
export declare const firstName: (input: import("fictional").JSONSerializable, options?: import("./oneOfString").OneOfStringOptions | undefined) => string; | ||
//# sourceMappingURL=firstName.d.ts.map |
@@ -1,2 +0,2 @@ | ||
export declare const lastName: (input: import("fictional").JSONSerializable, options?: import("./oneOfString").OneOfOptions) => any; | ||
export declare const lastName: (input: import("fictional").JSONSerializable, options?: import("./oneOfString").OneOfStringOptions | undefined) => string; | ||
//# sourceMappingURL=lastName.d.ts.map |
import { Input } from 'fictional'; | ||
import { Transform } from './types'; | ||
export interface OneOfOptions { | ||
type Fallback = string | Transform; | ||
export interface OneOfStringOptions { | ||
limit?: number; | ||
fallback?: Fallback; | ||
} | ||
export declare const oneOfString: (rawChoices: string[], fallback?: string | number | Transform) => (input: Input, options?: OneOfOptions) => any; | ||
export interface OneOfString { | ||
(choices: string[], fallback?: Fallback): (input: Input, options?: OneOfStringOptions) => string; | ||
(input: Input, choices: string[], options?: OneOfStringOptions): string; | ||
} | ||
export declare const oneOfString: OneOfString; | ||
export {}; | ||
//# sourceMappingURL=oneOfString.d.ts.map |
@@ -26,6 +26,23 @@ "use strict"; | ||
var import_primitives = require("./primitives"); | ||
const oneOfString = (...args) => { | ||
if (Array.isArray(args[1])) { | ||
const input = args[0]; | ||
const choices = args[1]; | ||
const options = args[2]; | ||
if ((options == null ? void 0 : options.limit) == null) { | ||
return (0, import_fictional.oneOf)(input, choices); | ||
} else { | ||
return oneOfStringImplementation(choices)(input, options); | ||
} | ||
} else { | ||
const choices = args[0]; | ||
const fallback = args[0]; | ||
return oneOfStringImplementation(choices, fallback); | ||
} | ||
}; | ||
const defaultFallback = import_primitives.word.options({ capitalize: false }); | ||
const oneOfString = (rawChoices, fallback = defaultFallback) => { | ||
const oneOfStringImplementation = (rawChoices, curriedFallback) => { | ||
const sortedChoices = rawChoices.slice().sort(compareByLength); | ||
const oneOfStringFn = (input, options = {}) => { | ||
const fallback = options.fallback ?? curriedFallback ?? defaultFallback; | ||
const { limit } = options; | ||
@@ -32,0 +49,0 @@ if (limit == null) { |
@@ -12,5 +12,5 @@ import { Input } from './types'; | ||
}) => void; | ||
export declare const expectGeneratedValue: <Result>(expectFn: (result: Result) => unknown, makerFn: (input: Input) => Result) => void; | ||
export declare const expectGeneratedValue: <Result>(expectFn: (result: Result, input: Input) => unknown, makerFn: (input: Input) => Result) => void; | ||
export declare const checkGeneratedValue: <Result>(predicateFn: (result: Result) => boolean, makerFn: (input: Input) => Result) => void; | ||
export declare function measureEntropy<V>(data: V[]): number; | ||
//# sourceMappingURL=testutils.d.ts.map |
@@ -47,3 +47,3 @@ "use strict"; | ||
oneOf: (input) => import__.copycat.oneOf(input, ["red", "green", "blue"]), | ||
oneOfString: (input) => import__.copycat.oneOfString(["red", "green", "blue"])(input), | ||
oneOfString: (input) => import__.copycat.oneOfString(input, ["red", "green", "blue"], { limit: 3 }), | ||
someOf: (input) => import__.copycat.someOf(input, [1, 2], ["rock", "paper", "scissors"]), | ||
@@ -78,3 +78,3 @@ scramble: (input) => import__.copycat.scramble(import__.copycat.fullName(input)) | ||
try { | ||
expectFn(result); | ||
expectFn(result, input); | ||
} catch (e) { | ||
@@ -81,0 +81,0 @@ if (e instanceof Error) { |
{ | ||
"name": "@snaplet/copycat", | ||
"version": "4.0.0", | ||
"version": "4.1.0", | ||
"description": "", | ||
@@ -32,5 +32,7 @@ "main": "dist/index.js", | ||
"fictional": "^1.0.0", | ||
"string-argv": "^0.3.2", | ||
"uuid": "^8.3.2" | ||
}, | ||
"devDependencies": { | ||
"@expo/spawn-async": "^1.7.2", | ||
"@types/jest": "^27.4.1", | ||
@@ -37,0 +39,0 @@ "@types/node": "^17.0.27", |
@@ -185,3 +185,3 @@ # ![copycat](https://user-images.githubusercontent.com/1731223/167850970-584e6953-6543-4085-af5a-f9d8b7ffe988.png) | ||
### `copycat.oneOf(input, values)` | ||
### `copycat.oneOf(input, values[, options])` | ||
@@ -195,2 +195,27 @@ Takes in an [`input`](#input) value and an array of `values`, and returns an item in `values` that corresponds to that `input`: | ||
### `copycat.oneOfString(input, values[, options])` | ||
Like `oneOf()`, takes in an [`input`](#input) value and an array of `values`, and returns an item in `values` that corresponds to that `input`. However, `values` needs to be an array of _string_ values, and only values within the character limit set by the `limit` option will be picked. | ||
```js | ||
copycat.oneOfString('foo', ['short', 'loooooooong'], { limit: 6 }) | ||
// => 'short' | ||
``` | ||
#### `options` | ||
- **`limit`**: If the `values` are strings, the picked value will be constrained to be less than `limit`'s amount of characters | ||
- **`fallback: string | (input) => string`**: When `limit` is specified but no values match the given `limit`, fallback is called with the given input value. | ||
#### `options` | ||
- **`limit`**: If the `values` are strings, the picked value will be constrained to be less than or equal `limit`'s amount of characters | ||
- **`fallback: string | `**: When `limit` is specified but no values match the given `limit`, fallback will be used. | ||
```js | ||
copycat.oneOfString('foo', ['short', 'loooooooong'], { limit: 2 }) | ||
// => 'ta' | ||
``` | ||
### `copycat.someOf(input, range, values)` | ||
@@ -197,0 +222,0 @@ |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
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
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
1892479
76556
716
4
23
+ Addedstring-argv@^0.3.2
+ Addedstring-argv@0.3.2(transitive)