Skip to content

Commit

Permalink
fix(insertAt): allow empty string to be inserted
Browse files Browse the repository at this point in the history
  • Loading branch information
Thanga-Ganapathy committed May 10, 2023
1 parent eaa37e5 commit 34140c6
Show file tree
Hide file tree
Showing 7 changed files with 27 additions and 27 deletions.
5 changes: 5 additions & 0 deletions .changeset/few-cups-wash.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@opentf/utils": minor
---

Fixed insertAt to allow empty string to be inserted.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@

[![Build](https://github.com/open-tech-foundation/js-utils/actions/workflows/build.yml/badge.svg)](https://github.com/open-tech-foundation/js-utils/actions/workflows/build.yml)

> A Collection of JavaScript Utility Functions.
> A collection of JavaScript utility functions.
## [Playground](https://js-utils.pages.dev/playground)

Expand Down
25 changes: 15 additions & 10 deletions apps/website/docs/String/insertAt.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -5,29 +5,34 @@
```ts
import { insertAt } from '@opentf/utils';

insertAt(str: string, index: number, insertStr?: string): string;
insertAt(str: string, index?: number, insertStr?: string): string;
```

:::info
Default index = 0
:::


## Examples

```ts
insertAt() // It throws an error;

insertAt('')) // ''
insertAt('') //=> ''

insertAt('', 0)) // ''
insertAt('', 0) //=> ''

insertAt('', 1, 'abc')) // ''
insertAt('', 1, 'abc') //=> 'abc'

insertAt('a', 0, 'b')) // 'ba'
insertAt('a', 0, 'b') //=> 'ba'

insertAt('a', 1, 'b')) // 'ab'
insertAt('a', 1, 'b') //=> 'ab'

insertAt('a', 1, 'bc')) // 'abc'
insertAt('a', 1, 'bc') //=> 'abc'

insertAt('foo baz', 3, ' bar')) // 'foo bar baz'
insertAt('foo baz', 3, ' bar') //=> 'foo bar baz'

insertAt('I want apple', 6, ' an')) // 'I want an apple'
insertAt('IU', 1, '❤️') //=> 'I❤️U'

insertAt('IU', 1, '❤️')) // 'I❤️U'
insertAt('I 🍊', 2, 'ate an') //=> 'I ate an 🍊'
```
2 changes: 1 addition & 1 deletion packages/utils/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@

[![Build](https://github.com/open-tech-foundation/js-utils/actions/workflows/build.yml/badge.svg)](https://github.com/open-tech-foundation/js-utils/actions/workflows/build.yml)

> A Collection of JavaScript Utility Functions.
> A collection of JavaScript utility functions.
## [Playground](https://js-utils.pages.dev/playground)

Expand Down
4 changes: 2 additions & 2 deletions packages/utils/__tests__/string/insertAt.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,12 @@ describe('String', () => {
expect(() => insertAt()).toThrow();
expect(insertAt('')).toBe('');
expect(insertAt('', 0)).toBe('');
expect(insertAt('', 1, 'abc')).toBe('');
expect(insertAt('', 1, 'abc')).toBe('abc');
expect(insertAt('a', 0, 'b')).toBe('ba');
expect(insertAt('a', 1, 'b')).toBe('ab');
expect(insertAt('a', 1, 'bc')).toBe('abc');
expect(insertAt('foo baz', 3, ' bar')).toBe('foo bar baz');
expect(insertAt('I want apple', 6, ' an')).toBe('I want an apple');
expect(insertAt('I 🍊', 2, 'ate an')).toBe('I ate an 🍊');
expect(insertAt('IU', 1, '❤️')).toBe('I❤️U');
});
});
2 changes: 1 addition & 1 deletion packages/utils/package.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"name": "@opentf/utils",
"version": "0.17.0",
"description": "A Collection of JavaScript Utility Functions.",
"description": "A collection of JavaScript utility functions.",
"keywords": [
"collection",
"js",
Expand Down
14 changes: 2 additions & 12 deletions packages/utils/src/string/insertAt.ts
Original file line number Diff line number Diff line change
@@ -1,15 +1,5 @@
function insertAt(str: string, index: number, insertStr = ''): string {
if (str.length === 0) {
return '';
}

const result = str.substring(0, index) + insertStr + str.substring(index);

if (typeof index !== 'number') {
return str;
}

return result;
function insertAt(str: string, index = 0, insertStr = ''): string {
return str.slice(0, index) + insertStr + str.slice(index);
}

export default insertAt;

0 comments on commit 34140c6

Please sign in to comment.