Skip to content

Commit

Permalink
implement string.repeat
Browse files Browse the repository at this point in the history
  • Loading branch information
dmonad committed Apr 22, 2023
1 parent 9866067 commit 4538c9f
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 0 deletions.
8 changes: 8 additions & 0 deletions string.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import * as array from './array.js'

/**
* Utility module to work with strings.
*
Expand Down Expand Up @@ -121,3 +123,9 @@ export const decodeUtf8 = utf8TextDecoder ? _decodeUtf8Native : _decodeUtf8Polyf
* @param {string} insert New content to insert
*/
export const splice = (str, index, remove, insert = '') => str.slice(0, index) + insert + str.slice(index + remove)

/**
* @param {string} source
* @param {number} n
*/
export const repeat = (source, n) => array.unfold(n, () => source).join('')
9 changes: 9 additions & 0 deletions string.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,15 @@ import * as prng from './prng.js'
import * as string from './string.js'
import * as t from './testing.js'

/**
* @param {t.TestCase} tc
*/
export const testUtilities = tc => {
t.assert(string.repeat('1', 3) === '111')
t.assert(string.repeat('1', 0) === '')
t.assert(string.repeat('1', 1) === '1')
}

/**
* @param {t.TestCase} tc
*/
Expand Down

0 comments on commit 4538c9f

Please sign in to comment.