Skip to content

Commit

Permalink
fix(replaceAt): allow empty string to be replaced
Browse files Browse the repository at this point in the history
  • Loading branch information
Thanga-Ganapathy committed May 10, 2023
1 parent ae47b50 commit 648dc19
Show file tree
Hide file tree
Showing 9 changed files with 546 additions and 375 deletions.
4 changes: 3 additions & 1 deletion .changeset/config.json
Original file line number Diff line number Diff line change
Expand Up @@ -7,5 +7,7 @@
"access": "public",
"baseBranch": "main",
"updateInternalDependencies": "patch",
"ignore": []
"ignore": ["website"],
"privatePackages": false,
"bumpVersionsWithWorkspaceProtocolOnly": true
}
5 changes: 5 additions & 0 deletions .changeset/wild-toys-obey.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@opentf/utils": minor
---

Fixed `replaceAt` to accept empty string to be replaced.
34 changes: 20 additions & 14 deletions apps/website/docs/String/replaceAt.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -5,35 +5,41 @@
```ts
import { replaceAt } from '@opentf/utils';

replaceAt(str: string, index: number, replaceStr?: string): string;
replaceAt(str: string, index?: number, replaceStr?: string): string;
```

:::info
Default index = 0
:::

## Examples

```ts
replaceAt() // It throws an error;
replaceAt(); // Throws error

replaceAt(""); //=> ''

replaceAt('')) // ''
replaceAt("", 0); //=> ''

replaceAt('', 0)) // ''
replaceAt("", 0, "a"); //=> 'a'

replaceAt('', 1, 'abc')) // ''
replaceAt("", 1, "abc"); //=> 'abc'

replaceAt('a')) // 'a'
replaceAt("a"); //=> ''

replaceAt('abc')) // 'abc'
replaceAt("abc"); //=> 'bc'

replaceAt('abc', 1)) // 'ac'
replaceAt("abc", 1); //=> 'ac'

replaceAt('abc', 5)) // 'abc'
replaceAt("abc", 5); //=> 'abc'

replaceAt('abc', 1, '')) // 'ac'
replaceAt("abc", 1, ""); //=> 'ac'

replaceAt('abc', 0, 'z')) // 'zbc'
replaceAt("abc", 0, "z"); //=> 'zbc'

replaceAt('iphone', 1, 'P')) // 'iPhone'
replaceAt("iphone", 1, "P"); //=> 'iPhone'

replaceAt('I__JS', 1, '❤️')) // 'I❤️JS'
replaceAt("I__JS", 1, "❤️"); //=> 'I❤️JS'

replaceAt('I HATE U', 2, 'LOVE')) // 'I LOVE U'
replaceAt("I HATE U", 2, "LOVE"); //=> 'I LOVE U'
```
2 changes: 1 addition & 1 deletion apps/website/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
"@docusaurus/core": "2.4.0",
"@docusaurus/preset-classic": "2.4.0",
"@mdx-js/react": "1.6.22",
"@opentf/react-sandbox": "^0.10.0",
"@opentf/react-sandbox": "^0.11.0",
"@svgr/webpack": "^6.5.1",
"clsx": "^1.2.1",
"file-loader": "^6.2.0",
Expand Down
2 changes: 1 addition & 1 deletion apps/website/src/pages/playground.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,4 +5,4 @@ title: Playground
import { SandBox } from '@opentf/react-sandbox';
import Source from '!!raw-loader!../Source.js';

<SandBox template="vanilla" code={Source} consoleType="Advanced" deps={["@opentf/utils"]} style={{height: '500px'}} />
<SandBox template="vanilla" code={Source} layout="Code_Console" consoleType="Advanced" deps={["@opentf/utils"]} style={{height: '500px'}} />
7 changes: 4 additions & 3 deletions packages/utils/__tests__/string/replaceAt.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,10 @@ describe('String', () => {
expect(() => replaceAt()).toThrow();
expect(replaceAt('')).toBe('');
expect(replaceAt('', 0)).toBe('');
expect(replaceAt('', 1, 'abc')).toBe('');
expect(replaceAt('a')).toBe('a');
expect(replaceAt('abc')).toBe('abc');
expect(replaceAt('', 0, 'a')).toBe('a');
expect(replaceAt('', 1, 'abc')).toBe('abc');
expect(replaceAt('a')).toBe('');
expect(replaceAt('abc')).toBe('bc');
expect(replaceAt('abc', 1)).toBe('ac');
expect(replaceAt('abc', 5)).toBe('abc');
expect(replaceAt('abc', 1, '')).toBe('ac');
Expand Down
16 changes: 8 additions & 8 deletions packages/utils/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -48,16 +48,16 @@
"@opentf/eslint-config-base": "^0.1.0",
"@swc/core": "^1.3.36",
"@swc/jest": "^0.2.24",
"@types/jest": "^29.4.0",
"@typescript-eslint/eslint-plugin": "^5.53.0",
"@typescript-eslint/parser": "^5.53.0",
"eslint": "^8.35.0",
"eslint-config-prettier": "^8.6.0",
"@types/jest": "^29.5.1",
"@typescript-eslint/eslint-plugin": "^5.59.5",
"@typescript-eslint/parser": "^5.59.5",
"eslint": "^8.40.0",
"eslint-config-prettier": "^8.8.0",
"eslint-plugin-jest": "^27.2.1",
"eslint-plugin-prettier": "^4.2.1",
"jest": "^29.4.3",
"jest": "^29.5.0",
"prettier": "^2.8.4",
"tsup": "^6.6.3",
"typescript": "^4.9.5"
"tsup": "^6.7.0",
"typescript": "^5.0.4"
}
}
14 changes: 2 additions & 12 deletions packages/utils/src/string/replaceAt.ts
Original file line number Diff line number Diff line change
@@ -1,17 +1,7 @@
function replaceAt(str: string, index: number, replaceStr = ''): string {
function replaceAt(str: string, index = 0, replaceStr = ''): string {
const rStrLen = index + (replaceStr.length === 0 ? 1 : replaceStr.length);

if (str.length === 0) {
return '';
}

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

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

return result;
return str.slice(0, index) + replaceStr + str.slice(rStrLen);
}

export default replaceAt;

0 comments on commit 648dc19

Please sign in to comment.