Skip to content

Commit

Permalink
feat: add repeat()
Browse files Browse the repository at this point in the history
  • Loading branch information
ersimont committed Dec 14, 2019
1 parent b45586e commit e1b6294
Show file tree
Hide file tree
Showing 7 changed files with 51 additions and 0 deletions.
1 change: 1 addition & 0 deletions projects/micro-dash/src/lib/string/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ export { lowerFirst } from "./lower-first";
export { pad } from "./pad";
export { padStart } from "./pad-start";
export { padEnd } from "./pad-end";
export { repeat } from "./repeat";
export { snakeCase } from "./snake-case";
export { toLower } from "./to-lower";
export { upperFirst } from "./upper-first";
Expand Down
27 changes: 27 additions & 0 deletions projects/micro-dash/src/lib/string/repeat.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
import { repeat } from "./repeat";

describe("repeat()", () => {
//
// stolen from https://github.com/lodash/lodash
//

const str = "abc";

it("should repeat a string `n` times", () => {
expect(repeat("*", 3)).toBe("***");
expect(repeat(str, 2)).toBe("abcabc");
});

it("should return an empty string if `n` is <= `0`", () => {
expect(repeat(str, 0)).toBe("");
expect(repeat(str, -2)).toBe("");
});

it("should coerce `n` to an integer", () => {
expect(repeat(str, 2.6)).toBe("abcabc");
});

it("should return an empty string for empty values", () => {
expect(repeat("", 2)).toBe("");
});
});
15 changes: 15 additions & 0 deletions projects/micro-dash/src/lib/string/repeat.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
/**
* Repeats the given string `n` times.
*
* Differences from lodash:
* - does not work as an iteratee for methods like `map`
*
* Contribution to minified bundle size, when it is the only function imported:
* - Lodash: 2,467 bytes
* - Micro-dash: 62 bytes
*/
// tslint:disable-next-line:variable-name
export function repeat(string: string, n: number) {
// tslint:disable-next-line:no-bitwise
return n < 0 ? "" : new Array(n | 0).fill(string).join("");
}
1 change: 1 addition & 0 deletions projects/sizes/src/lib/string/index.lodash.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import "./lower-first.lodash";
import "./pad-end.lodash";
import "./pad-start.lodash";
import "./pad.lodash";
import "./repeat.lodash";
import "./snake-case.lodash";
import "./to-lower.lodash";
import "./upper-first.lodash";
Expand Down
1 change: 1 addition & 0 deletions projects/sizes/src/lib/string/index.microdash.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import "./lower-first.microdash";
import "./pad-end.microdash";
import "./pad-start.microdash";
import "./pad.microdash";
import "./repeat.microdash";
import "./snake-case.microdash";
import "./to-lower.microdash";
import "./upper-first.microdash";
Expand Down
3 changes: 3 additions & 0 deletions projects/sizes/src/lib/string/repeat.lodash.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
import repeat from "lodash-es/repeat";

console.log(repeat("hi", 3), repeat("", -1));
3 changes: 3 additions & 0 deletions projects/sizes/src/lib/string/repeat.microdash.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
import { repeat } from "micro-dash";

console.log(repeat("hi", 3), repeat("", -1));

0 comments on commit e1b6294

Please sign in to comment.