diff --git a/packages/transducers/README.md b/packages/transducers/README.md index 1b0124138d..a151ea9efe 100644 --- a/packages/transducers/README.md +++ b/packages/transducers/README.md @@ -813,11 +813,7 @@ itself. Returns nothing. #### `vals(x: IObjectOf): IterableIterator` -#### `wrapBoth(src: T[], n?: number): IterableIterator` - -#### `wrapLeft(src: T[], n?: number): IterableIterator` - -#### `wrapRight(src: T[], n?: number): IterableIterator` +#### `wrap(src: T[], n = 1, left = true, right = true): IterableIterator` ## Authors diff --git a/packages/transducers/src/index.ts b/packages/transducers/src/index.ts index 22c6ba0ba1..ec66127ecc 100644 --- a/packages/transducers/src/index.ts +++ b/packages/transducers/src/index.ts @@ -124,6 +124,4 @@ export * from "./iter/repeatedly"; export * from "./iter/reverse"; export * from "./iter/tuples"; export * from "./iter/vals"; -export * from "./iter/wrap-both"; -export * from "./iter/wrap-left"; -export * from "./iter/wrap-right"; +export * from "./iter/wrap"; diff --git a/packages/transducers/src/iter/wrap-both.ts b/packages/transducers/src/iter/wrap-both.ts index 2f3a3fcfc7..8ccc373dd1 100644 --- a/packages/transducers/src/iter/wrap-both.ts +++ b/packages/transducers/src/iter/wrap-both.ts @@ -1,21 +1,12 @@ -import { illegalArgs } from "@thi.ng/errors/illegal-arguments"; +import { wrap } from "./wrap"; /** - * Combination of `wrapLeft()` and `wrapRight()`. Yields iterator of - * `src` with the last `n` values of `src` prepended at the beginning - * and the first `n` values appended at the end. Throws error if `n` < 0 - * or larger than `src.length`. + * See `wrap()`. * + * @deprecated superceded by `wrap()` * @param src * @param n */ -export function* wrapBoth(src: T[], n = 1) { - (n < 0 || n > src.length) && illegalArgs(`wrong number of wrap items: got ${n} max: ${src.length}`); - for (let m = src.length, i = m - n; i < m; i++) { - yield src[i]; - } - yield* src; - for (let i = 0; i < n; i++) { - yield src[i]; - } +export function wrapBoth(src: T[], n = 1) { + return wrap(src, n); } diff --git a/packages/transducers/src/iter/wrap-left.ts b/packages/transducers/src/iter/wrap-left.ts index e49ee658de..57d9a7a7f0 100644 --- a/packages/transducers/src/iter/wrap-left.ts +++ b/packages/transducers/src/iter/wrap-left.ts @@ -1,17 +1,12 @@ -import { illegalArgs } from "@thi.ng/errors/illegal-arguments"; +import { wrap } from "./wrap"; /** - * Yields iterator of `src` with the last `n` values of `src` prepended - * at the beginning. Throws error if `n` < 0 or larger than - * `src.length`. + * See `wrap()`. * + * @deprecated superceded by `wrap()` * @param src * @param n */ -export function* wrapLeft(src: T[], n = 1) { - (n < 0 || n > src.length) && illegalArgs(`wrong number of wrap items: got ${n} max: ${src.length}`); - for (let m = src.length, i = m - n; i < m; i++) { - yield src[i]; - } - yield* src; +export function wrapLeft(src: T[], n = 1) { + return wrap(src, n, true, false); } diff --git a/packages/transducers/src/iter/wrap-right.ts b/packages/transducers/src/iter/wrap-right.ts index 5c59795920..50903dd153 100644 --- a/packages/transducers/src/iter/wrap-right.ts +++ b/packages/transducers/src/iter/wrap-right.ts @@ -1,16 +1,12 @@ -import { illegalArgs } from "@thi.ng/errors/illegal-arguments"; +import { wrap } from "./wrap"; /** - * Yields iterator of `src` with the first `n` values of `src` appended - * at the end. Throws error if `n` < 0 or larger than `src.length`. + * See `wrap()`. * + * @deprecated superceded by `wrap()` * @param src * @param n */ -export function* wrapRight(src: T[], n = 1) { - (n < 0 || n > src.length) && illegalArgs(`wrong number of wrap items: got ${n} max: ${src.length}`); - yield* src; - for (let i = 0; i < n; i++) { - yield src[i]; - } +export function wrapRight(src: T[], n = 1) { + return wrap(src, n, false, true); } diff --git a/packages/transducers/src/iter/wrap.ts b/packages/transducers/src/iter/wrap.ts new file mode 100644 index 0000000000..2504825470 --- /dev/null +++ b/packages/transducers/src/iter/wrap.ts @@ -0,0 +1,27 @@ +import { illegalArgs } from "@thi.ng/errors/illegal-arguments"; + +/** + * Yields iterator of `src` with the last `n` values of `src` prepended + * at the beginning (if `left` is truthy) and/or the first `n` values + * appended at the end (if `right` is truthy). Wraps both sides by + * default and throws error if `n` < 0 or larger than `src.length`. + * + * @param src + * @param n + * @param left + * @param right + */ +export function* wrap(src: T[], n = 1, left = true, right = true) { + (n < 0 || n > src.length) && illegalArgs(`wrong number of wrap items: got ${n} max: ${src.length}`); + if (left) { + for (let m = src.length, i = m - n; i < m; i++) { + yield src[i]; + } + } + yield* src; + if (right) { + for (let i = 0; i < n; i++) { + yield src[i]; + } + } +}