Skip to content

Commit

Permalink
fix: add deleteCount param to arrReplace
Browse files Browse the repository at this point in the history
  • Loading branch information
Thanga-Ganapathy committed Apr 13, 2024
1 parent 53e4dec commit 1de5642
Show file tree
Hide file tree
Showing 7 changed files with 52 additions and 18 deletions.
5 changes: 5 additions & 0 deletions .changeset/brown-turkeys-guess.md
@@ -0,0 +1,5 @@
---
"@opentf/std": minor
---

Fixed arrReplace by adding deleteCount param.
18 changes: 11 additions & 7 deletions apps/docs/pages/Array/arrReplace.mdx
Expand Up @@ -14,7 +14,8 @@ import { arrReplace } from '@opentf/std';

arrReplace<T>(
arr: T[] = [],
index?: number | null,
index: number | null,
deleteCount: number | null,
...replacements: T[]
)
```
Expand All @@ -26,20 +27,23 @@ arrReplace([]) // => []

arrReplace([1]) // => [1]

arrReplace([1, 2, 3], null, 'a') // => [1, 2, 'a']
arrReplace([1, 2, 3], 0, 1, 'a') // => ['a', 2, 3]

arrReplace([1, 2, 3], 0, 'a') // => ['a', 2, 3]
arrReplace([1, 2, 3], null, 1, 'a') // => [1, 2, 'a']

arrReplace([1, 2, 3], 1, 'a', 'b') // => [1, 'a', 'b']
arrReplace([1, 2, 3], 3, null, 'c') // => [1, 2, 3, 'c']

arrReplace([1, 2, 3], 3, 'c') // => [1, 2, 3, 'c']
const months = ['Jan', 'Feb', 'Apr', 'May'];
arrReplace(months, 1, 1, 'Feb', 'Mar') //=> ['Jan', 'Feb', 'Mar', 'Apr', 'May']

arrReplace([1, 2, 3], 5, 'c', 'd') // => [1, 2, 3, 'c', 'd']
arrReplace(months, 1, null, 'Feb', 'Mar') //=> ['Jan', 'Feb', 'Mar', 'May']
```

## Try

<REPL code={`const { arrReplace } = require('@opentf/std');
arrReplace([1, 2, 3], 1, 'Two');
const months = ['Jan', 'Feb', 'Apr', 'May'];
arrReplace(months, 1, 1, 'Feb', 'Mar');
`} />
2 changes: 1 addition & 1 deletion apps/docs/pages/String/strReplace.mdx
Expand Up @@ -11,7 +11,7 @@ import { strReplace } from '@opentf/std';
strReplace(str: string,
pattern: string | RegExp,
replacement: string | Function,
options: {all: boolean, case: boolean}
options?: {all: boolean, case: boolean}
): string;
```

Expand Down
31 changes: 26 additions & 5 deletions packages/std/__tests__/array/arrReplace.spec.ts
Expand Up @@ -5,10 +5,31 @@ describe('Array', () => {
expect(arrReplace()).toEqual([]);
expect(arrReplace([])).toEqual([]);
expect(arrReplace([1])).toEqual([1]);
expect(arrReplace([1, 2, 3], null, 'a')).toEqual([1, 2, 'a']);
expect(arrReplace([1, 2, 3], 0, 'a')).toEqual(['a', 2, 3]);
expect(arrReplace([1, 2, 3], 1, 'a', 'b')).toEqual([1, 'a', 'b']);
expect(arrReplace([1, 2, 3], 3, 'c')).toEqual([1, 2, 3, 'c']);
expect(arrReplace([1, 2, 3], 5, 'c', 'd')).toEqual([1, 2, 3, 'c', 'd']);
expect(arrReplace([1, 2, 3], null, null, 'a')).toEqual([1, 2, 'a']);
expect(arrReplace([1, 2, 3], 0, null, 'a')).toEqual(['a', 2, 3]);
expect(arrReplace([1, 2, 3], 1, null, 'a', 'b')).toEqual([1, 'a', 'b']);
expect(arrReplace([1, 2, 3], 3, null, 'c')).toEqual([1, 2, 3, 'c']);
expect(arrReplace([1, 2, 3], 5, null, 'c', 'd')).toEqual([
1,
2,
3,
'c',
'd',
]);

const months = ['Jan', 'Feb', 'Apr', 'May'];
expect(arrReplace(months, 1, 1, 'Feb', 'Mar')).toEqual([
'Jan',
'Feb',
'Mar',
'Apr',
'May',
]);
expect(arrReplace(months, 1, null, 'Feb', 'Mar')).toEqual([
'Jan',
'Feb',
'Mar',
'May',
]);
});
});
2 changes: 1 addition & 1 deletion packages/std/jsr.json
@@ -1,5 +1,5 @@
{
"name": "@opentf/std",
"version": "0.7.0",
"version": "0.8.0",
"exports": "./src/index.ts"
}
5 changes: 3 additions & 2 deletions packages/std/src/array/arrReplace.ts
Expand Up @@ -7,13 +7,14 @@
*/
export default function arrReplace<T>(
arr: T[] = [],
index?: number | null,
index: number | null,
deleteCount: number | null,
...replacements: T[]
): T[] {
const idx = index ?? arr.length - 1;
const a = [...arr];

a.splice(idx, replacements.length, ...replacements);
a.splice(idx, deleteCount ?? replacements.length, ...replacements);

return a;
}
7 changes: 5 additions & 2 deletions packages/std/src/string/strReplace.ts
Expand Up @@ -14,10 +14,13 @@ export default function strReplace(
str: string,
pattern: string | RegExp,
replacement: string,
options: StrReplaceOptions
options?: StrReplaceOptions
): string {
const defaultOptions: StrReplaceOptions = { all: false, case: false };
const opts = shallowMerge(defaultOptions, options) as StrReplaceOptions;
const opts = shallowMerge(
defaultOptions,
options as object
) as StrReplaceOptions;
let flags = '';
flags = opts.all ? flags + 'g' : flags;
flags = opts.case ? flags + 'i' : flags;
Expand Down

0 comments on commit 1de5642

Please sign in to comment.