Skip to content

Commit

Permalink
feat(transducers): add randomID()
Browse files Browse the repository at this point in the history
  • Loading branch information
postspectacular committed Sep 17, 2018
1 parent f001314 commit b488d2b
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 0 deletions.
22 changes: 22 additions & 0 deletions packages/transducers/src/func/random-id.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
import { choices } from "../iter/choices";
import { take } from "../xform/take";

/**
* Generates and returns a random string of `len` characters (default
* 4), plus optional given `prefix` and using only provided `syms`
* characters (default lowercase a-z).
*
* ```
* randomID()
* "qgdt"
*
* randomID(8, "id-", "0123456789ABCDEF")
* "id-94EF6E1A"
* ```
*
* @param len
* @param prefix
* @param syms
*/
export const randomID = (len = 4, prefix = "", syms = "abcdefghijklmnopqrstuvwxyz") =>
[prefix, ...take(len, choices(syms))].join("");
1 change: 1 addition & 0 deletions packages/transducers/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -115,6 +115,7 @@ export * from "./func/key-selector";
export * from "./func/lookup";
export * from "./func/odd";
export * from "./func/peek";
export * from "./func/random-id";
export * from "./func/renamer";
export * from "./func/swizzler";
export * from "./func/weighted-random";
Expand Down

0 comments on commit b488d2b

Please sign in to comment.