···11+MIT License
22+33+Copyright (c) 2023 Matt Foxx
44+55+Permission is hereby granted, free of charge, to any person obtaining a copy
66+of this software and associated documentation files (the "Software"), to deal
77+in the Software without restriction, including without limitation the rights
88+to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
99+copies of the Software, and to permit persons to whom the Software is
1010+furnished to do so, subject to the following conditions:
1111+1212+The above copyright notice and this permission notice shall be included in all
1313+copies or substantial portions of the Software.
1414+1515+THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
1616+IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
1717+FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
1818+AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
1919+LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
2020+OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
2121+SOFTWARE.
+67
README.md
···11+# redact-string
22+33+Redact (replace) part or all of a string with a character.
44+55+## Why another react package?
66+77+The other popular packages seem to be restricted to a certain behavior:
88+99+* Redaction of objects/json (instead of plain variables/values you control)
1010+* Redaction **entirely** replaces value leaving no structure or hint of what it was
1111+1212+This didn't fit my needs. I wanted to be able to control how many characters were replaced as well as leave part of the structure intact for hinting in logging. A prime example being replacing _part_ of an IP address so users can view/post logs which confirm correct settings but don't give away full addresses IE `192.168.1.105` -> `**********105`
1313+1414+# Install/Usage
1515+1616+```
1717+npm install @foxxmd/react-string
1818+```
1919+2020+```js
2121+import {reactString} from '@foxxmd/redact-string';
2222+2323+console.log(reactString('192.168.1.105', 3)); // 3 is the number of character to leave visible
2424+// *********105
2525+```
2626+2727+# Options
2828+2929+An optional, third argument can be passed that defines how the redact occurs:
3030+3131+```ts
3232+export interface RedactOptions {
3333+ /**
3434+ * Replace characters starting at the start or end of string (default start)
3535+ * */
3636+ replaceFrom?: 'start' | 'end'
3737+ /**
3838+ * The character/string that characters are replaced with (default '*')
3939+ * */
4040+ replaceWith?: string
4141+ /**
4242+ * Which type of characters to replace in the string (default any)
4343+ * */
4444+ replace?: 'any' | 'alphanumeric' | 'alpha' | 'numeric'
4545+}
4646+```
4747+4848+# Examples
4949+5050+```js
5151+5252+// replace all but last 3 characters
5353+console.log(reactString('192.168.1.105', 3));
5454+// *********105
5555+5656+// replace all but first 3 characters
5757+console.log(reactString('192.168.1.105', 3, {replaceFrom: 'end'}));
5858+// 192**********
5959+6060+// replace all but last 3 characters, numeric only
6161+console.log(reactString('192.168.1.105', 3, {replace: 'numeric'}));
6262+// ***.***.*.105
6363+6464+// replace all but last 5 characters with 'X'
6565+console.log(reactString('superSecretPassword', 5, {replaceWith: 'X'}));
6666+// XXXXXXXXXXXXXXsword
6767+```