Socket
Socket
Sign inDemoInstall

@handy-common-utils/misc-utils

Package Overview
Dependencies
Maintainers
1
Versions
16
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

@handy-common-utils/misc-utils - npm Package Compare versions

Comparing version 1.3.1 to 1.4.0

dist/substitute.d.ts

1

dist/index.d.ts

@@ -7,2 +7,3 @@ export * from './codec';

export * from './array';
export * from './substitute';
//# sourceMappingURL=index.d.ts.map

@@ -10,1 +10,2 @@ "use strict";

tslib_1.__exportStar(require("./array"), exports);
tslib_1.__exportStar(require("./substitute"), exports);

2

package.json
{
"name": "@handy-common-utils/misc-utils",
"version": "1.3.1",
"version": "1.4.0",
"description": "Miscellaneous utilities",

@@ -5,0 +5,0 @@ "scripts": {

@@ -138,2 +138,42 @@ # @handy-common-utils/misc-utils

## substituteAll
The `substituteAll(input, searchPattern, substitute)` function allows you to perform substitutions on an input string
by matching a specified pattern and replacing the matches with substitution strings built by a function.
It provides flexibility in handling complex substitution scenarios through the `substitute` callback function.
### Example Usage Scenarios:
- __Templating__: Replace placeholder variables in a template string with dynamic values. For example, transforming the template "Hello, {name}! How are you, {name}? I am {me}." into "Hello, John! How are you, John? I am James." by substituting `{name}` with the value "John" and `{me}` with value "James".
```typescript
const input = 'Hello, {name}! How are you, {name}? I am {me}.';
const searchPattern = /{([^{}]+)}/g;
const dict: Record<string, string> = {
name: 'John',
me: 'James',
};
const substitute: Parameters<typeof substituteAll>[2] = (_match, result) => {
const key = result[1];
return dict[key] ?? `{NOT FOUND: ${key}}`;
};
const result = substituteAll(input, searchPattern, substitute);
```
- __Text Transformation__: Modify specific segments of a string based on predefined patterns. For instance, converting dates written in a non-standard format, such as "MM/DD/YY", to a standardized format, like "YYYY-MM-DD", using a suitable regular expression pattern and substitution logic.
```typescript
const input = 'Event date: 12/31/21';
const searchPattern = / ((\d{2})\/(\d{2})\/(\d{2}))/g;
const substitute = (_: string, result: any) => {
const [match, date, month, day, year] = result;
const formattedDate = `20${year}-${month}-${day}`;
return match.replace(date, formattedDate);
};
const result = substituteAll(input, searchPattern, substitute);
```
# API

@@ -155,2 +195,3 @@

- [stringify-replacer](#modulesstringify_replacermd)
- [substitute](#modulessubstitutemd)

@@ -859,2 +900,8 @@ ## Classes

##### substituteAll
Re-exports [substituteAll](#substituteall)
___
##### urlSafe

@@ -1168,2 +1215,35 @@

the replacer function built from those path based rules
<a name="modulessubstitutemd"></a>
### Module: substitute
#### Functions
##### substituteAll
▸ **substituteAll**<`T`\>(`input`, `searchPattern`, `substitute`): `T`
Substitute all occurrences of a pattern in a string.
###### Type parameters
| Name | Type |
| :------ | :------ |
| `T` | extends `undefined` \| ``null`` \| `string` |
###### Parameters
| Name | Type | Description |
| :------ | :------ | :------ |
| `input` | `T` | The input string on which the substitutions will be performed. |
| `searchPattern` | `RegExp` | The regular expression pattern used to search for segments that should be substituted. It must have the `g` flag set. If the beginning part of the `input` should be skipped, set the `lastIndex` of the `searchPattern` before calling this function. After all the substitution are done, the `lastIndex` of the `searchPattern` will be reset to zero. See [https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/RegExp/lastIndex](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/RegExp/lastIndex) |
| `substitute` | (`match`: `string`, `matchResult`: `RegExpExecArray`) => ``null`` \| `string` | TThe function that builds the substitution string. It is called with the matched substring and the result of `RegExp.exec()`. See [https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/RegExp/exec#examples](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/RegExp/exec#examples). The function can return null to indicate that no further substitution is desired. In such case, the `lastIndex` of the `searchPattern` will not be reset to zero. |
###### Returns
`T`
The resulting string after performing all substitutions.
<!-- API end -->

Sorry, the diff of this file is not supported yet

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc