[READ-ONLY] Mirror of https://github.com/FoxxMD/redact-string. Redact part or all of a string foxxmd.github.io/redact-string
javascript redact redaction replace-text string string-manipulation typescript
0

Configure Feed

Select the types of activity you want to include in your feed.

Initial commit

FoxxMD (Jul 11, 2023, 11:23 AM EDT) 90214b75

+320
+29
.github/workflows/typedoc_deploy.yml
··· 1 + # https://github.com/TypeStrong/typedoc/issues/1485#issuecomment-889376169 2 + name: Typedoc Deploy 3 + 4 + on: [push, pull_request] 5 + 6 + jobs: 7 + build_and_deploy: 8 + runs-on: ubuntu-latest 9 + steps: 10 + - name: Checkout the repository 11 + uses: actions/checkout@v3 12 + 13 + - name: Setup Node.js 14 + uses: actions/setup-node@v3 15 + with: 16 + node-version: 16.x 17 + cache: 'yarn' 18 + 19 + - run: yarn install --frozen-lockfile 20 + - run: yarn run build 21 + 22 + - name: Create the docs directory locally in CI 23 + run: npx typedoc src/index.ts 24 + 25 + - name: Deploy 🚀 26 + uses: JamesIves/github-pages-deploy-action@4.1.4 27 + with: 28 + branch: gh-pages 29 + folder: docs
+5
.gitignore
··· 1 + **/src/**/*.map 2 + **/src/**/*.js 3 + node_modules/ 4 + /.idea 5 + /docs
+1
.nvmrc
··· 1 + lts/gallium
+21
LICENSE
··· 1 + MIT License 2 + 3 + Copyright (c) 2023 Matt Foxx 4 + 5 + Permission is hereby granted, free of charge, to any person obtaining a copy 6 + of this software and associated documentation files (the "Software"), to deal 7 + in the Software without restriction, including without limitation the rights 8 + to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 + copies of the Software, and to permit persons to whom the Software is 10 + furnished to do so, subject to the following conditions: 11 + 12 + The above copyright notice and this permission notice shall be included in all 13 + copies or substantial portions of the Software. 14 + 15 + THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 + IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 + FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 + AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 + LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 + OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 21 + SOFTWARE.
+67
README.md
··· 1 + # redact-string 2 + 3 + Redact (replace) part or all of a string with a character. 4 + 5 + ## Why another react package? 6 + 7 + The other popular packages seem to be restricted to a certain behavior: 8 + 9 + * Redaction of objects/json (instead of plain variables/values you control) 10 + * Redaction **entirely** replaces value leaving no structure or hint of what it was 11 + 12 + 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` 13 + 14 + # Install/Usage 15 + 16 + ``` 17 + npm install @foxxmd/react-string 18 + ``` 19 + 20 + ```js 21 + import {reactString} from '@foxxmd/redact-string'; 22 + 23 + console.log(reactString('192.168.1.105', 3)); // 3 is the number of character to leave visible 24 + // *********105 25 + ``` 26 + 27 + # Options 28 + 29 + An optional, third argument can be passed that defines how the redact occurs: 30 + 31 + ```ts 32 + export interface RedactOptions { 33 + /** 34 + * Replace characters starting at the start or end of string (default start) 35 + * */ 36 + replaceFrom?: 'start' | 'end' 37 + /** 38 + * The character/string that characters are replaced with (default '*') 39 + * */ 40 + replaceWith?: string 41 + /** 42 + * Which type of characters to replace in the string (default any) 43 + * */ 44 + replace?: 'any' | 'alphanumeric' | 'alpha' | 'numeric' 45 + } 46 + ``` 47 + 48 + # Examples 49 + 50 + ```js 51 + 52 + // replace all but last 3 characters 53 + console.log(reactString('192.168.1.105', 3)); 54 + // *********105 55 + 56 + // replace all but first 3 characters 57 + console.log(reactString('192.168.1.105', 3, {replaceFrom: 'end'})); 58 + // 192********** 59 + 60 + // replace all but last 3 characters, numeric only 61 + console.log(reactString('192.168.1.105', 3, {replace: 'numeric'})); 62 + // ***.***.*.105 63 + 64 + // replace all but last 5 characters with 'X' 65 + console.log(reactString('superSecretPassword', 5, {replaceWith: 'X'})); 66 + // XXXXXXXXXXXXXXsword 67 + ```
+7
dist/index.d.ts
··· 1 + export interface RedactOptions { 2 + replaceFrom?: 'start' | 'end'; 3 + replaceWith?: string; 4 + replace?: 'any' | 'alphanumeric' | 'alpha' | 'numeric'; 5 + } 6 + export declare const redactString: (str: string, visibleCount: number, options?: RedactOptions) => string; 7 + export default redactString;
+20
dist/index.js
··· 1 + "use strict"; 2 + Object.defineProperty(exports, "__esModule", { value: true }); 3 + exports.redactString = void 0; 4 + const redactString = (str, visibleCount, options) => { 5 + const { replaceFrom = 'start', replaceWith = '*', replace = 'any' } = options || {}; 6 + let replaceChar = `\\S`; 7 + if (replace === 'alphanumeric') { 8 + replaceChar = `\\w`; 9 + } 10 + else if (replace === 'alpha') { 11 + replaceChar = `[a-zA-Z]`; 12 + } 13 + else if (replace === 'numeric') { 14 + replaceChar = '\\d'; 15 + } 16 + const reg = replaceFrom === 'start' ? new RegExp(`${replaceChar}(?=\\S{${visibleCount}})`, 'gi') : new RegExp(`(?<=\\S{${visibleCount}})${replaceChar}`, 'gi'); 17 + return str.replace(reg, replaceWith); 18 + }; 19 + exports.redactString = redactString; 20 + exports.default = exports.redactString;
+30
package.json
··· 1 + { 2 + "name": "@foxxmd/redact-string", 3 + "version": "0.1.1", 4 + "description": "Redact part or all of string by replacing character", 5 + "repository": "https://github.com/FoxxMD/redact-string", 6 + "author": "FoxxMD", 7 + "license": "MIT", 8 + "private": false, 9 + "engines": { 10 + "node": ">=16.20.0", 11 + "npm": ">=8.19.4" 12 + }, 13 + "main": "dist/index.js", 14 + "types": "dist/index.d.ts", 15 + "files": [ 16 + "/dist" 17 + ], 18 + "scripts": { 19 + "typedoc": "typedoc", 20 + "build": "tsc" 21 + }, 22 + "exports": { 23 + ".": "./dist/index.js" 24 + }, 25 + "devDependencies": { 26 + "@tsconfig/node16": "^1.0.3", 27 + "typedoc": "^0.23.28", 28 + "typescript": "^4.9.5" 29 + } 30 + }
+27
src/index.ts
··· 1 + export interface RedactOptions { 2 + replaceFrom?: 'start' | 'end' 3 + replaceWith?: string 4 + replace?: 'any' | 'alphanumeric' | 'alpha' | 'numeric' 5 + } 6 + 7 + export const redactString = (str: string, visibleCount: number, options?: RedactOptions) => { 8 + const { 9 + replaceFrom = 'start', 10 + replaceWith = '*', 11 + replace = 'any' 12 + } = options || {}; 13 + 14 + let replaceChar = `\\S`; 15 + if(replace === 'alphanumeric') { 16 + replaceChar = `\\w`; 17 + } else if(replace === 'alpha') { 18 + replaceChar = `[a-zA-Z]` 19 + } else if(replace === 'numeric') { 20 + replaceChar = '\\d'; 21 + } 22 + 23 + const reg: RegExp = replaceFrom === 'start' ? new RegExp(`${replaceChar}(?=\\S{${visibleCount}})`, 'gi') : new RegExp(`(?<=\\S{${visibleCount}})${replaceChar}`, 'gi') 24 + return str.replace(reg, replaceWith); 25 + } 26 + 27 + export default redactString;
+17
tsconfig.json
··· 1 + { 2 + "extends": "@tsconfig/node16/tsconfig.json", 3 + "compilerOptions": { 4 + "module": "commonjs", 5 + "declaration": true, 6 + "outDir": "./dist" 7 + }, 8 + "typeRoots": [ 9 + "./src/types.d.ts" 10 + ], 11 + "include": [ 12 + "src/**/*" 13 + ], 14 + "exclude": [ 15 + "node_modules" 16 + ] 17 + }
+14
typedoc.json
··· 1 + { 2 + "$schema": "https://typedoc.org/schema.json", 3 + "name": "string-sameness Docs", 4 + "entryPoints": ["./src"], 5 + "sort": ["source-order"], 6 + "categorizeByGroup": false, 7 + "searchGroupBoosts": { 8 + "Functions": 1.5 9 + }, 10 + "navigationLinks": { 11 + "Docs": "http://foxxmd.github.io/string-sameness", 12 + "GitHub": "https://github.com/foxxmd/string-sameness" 13 + } 14 + }
+82
yarn.lock
··· 1 + # THIS IS AN AUTOGENERATED FILE. DO NOT EDIT THIS FILE DIRECTLY. 2 + # yarn lockfile v1 3 + 4 + 5 + "@tsconfig/node16@^1.0.3": 6 + version "1.0.4" 7 + resolved "https://registry.yarnpkg.com/@tsconfig/node16/-/node16-1.0.4.tgz#0b92dcc0cc1c81f6f306a381f28e31b1a56536e9" 8 + integrity sha512-vxhUy4J8lyeyinH7Azl1pdd43GJhZH/tP2weN8TntQblOY+A0XbT8DJk1/oCPuOOyg/Ja757rG0CgHcWC8OfMA== 9 + 10 + ansi-sequence-parser@^1.1.0: 11 + version "1.1.0" 12 + resolved "https://registry.yarnpkg.com/ansi-sequence-parser/-/ansi-sequence-parser-1.1.0.tgz#4d790f31236ac20366b23b3916b789e1bde39aed" 13 + integrity sha512-lEm8mt52to2fT8GhciPCGeCXACSz2UwIN4X2e2LJSnZ5uAbn2/dsYdOmUXq0AtWS5cpAupysIneExOgH0Vd2TQ== 14 + 15 + balanced-match@^1.0.0: 16 + version "1.0.2" 17 + resolved "https://registry.yarnpkg.com/balanced-match/-/balanced-match-1.0.2.tgz#e83e3a7e3f300b34cb9d87f615fa0cbf357690ee" 18 + integrity sha512-3oSeUO0TMV67hN1AmbXsK4yaqU7tjiHlbxRDZOpH0KW9+CeX4bRAaX0Anxt0tx2MrpRpWwQaPwIlISEJhYU5Pw== 19 + 20 + brace-expansion@^2.0.1: 21 + version "2.0.1" 22 + resolved "https://registry.yarnpkg.com/brace-expansion/-/brace-expansion-2.0.1.tgz#1edc459e0f0c548486ecf9fc99f2221364b9a0ae" 23 + integrity sha512-XnAIvQ8eM+kC6aULx6wuQiwVsnzsi9d3WxzV3FpWTGA19F621kwdbsAcFKXgKUHZWsy+mY6iL1sHTxWEFCytDA== 24 + dependencies: 25 + balanced-match "^1.0.0" 26 + 27 + jsonc-parser@^3.2.0: 28 + version "3.2.0" 29 + resolved "https://registry.yarnpkg.com/jsonc-parser/-/jsonc-parser-3.2.0.tgz#31ff3f4c2b9793f89c67212627c51c6394f88e76" 30 + integrity sha512-gfFQZrcTc8CnKXp6Y4/CBT3fTc0OVuDofpre4aEeEpSBPV5X5v4+Vmx+8snU7RLPrNHPKSgLxGo9YuQzz20o+w== 31 + 32 + lunr@^2.3.9: 33 + version "2.3.9" 34 + resolved "https://registry.yarnpkg.com/lunr/-/lunr-2.3.9.tgz#18b123142832337dd6e964df1a5a7707b25d35e1" 35 + integrity sha512-zTU3DaZaF3Rt9rhN3uBMGQD3dD2/vFQqnvZCDv4dl5iOzq2IZQqTxu90r4E5J+nP70J3ilqVCrbho2eWaeW8Ow== 36 + 37 + marked@^4.2.12: 38 + version "4.3.0" 39 + resolved "https://registry.yarnpkg.com/marked/-/marked-4.3.0.tgz#796362821b019f734054582038b116481b456cf3" 40 + integrity sha512-PRsaiG84bK+AMvxziE/lCFss8juXjNaWzVbN5tXAm4XjeaS9NAHhop+PjQxz2A9h8Q4M/xGmzP8vqNwy6JeK0A== 41 + 42 + minimatch@^7.1.3: 43 + version "7.4.6" 44 + resolved "https://registry.yarnpkg.com/minimatch/-/minimatch-7.4.6.tgz#845d6f254d8f4a5e4fd6baf44d5f10c8448365fb" 45 + integrity sha512-sBz8G/YjVniEz6lKPNpKxXwazJe4c19fEfV2GDMX6AjFz+MX9uDWIZW8XreVhkFW3fkIdTv/gxWr/Kks5FFAVw== 46 + dependencies: 47 + brace-expansion "^2.0.1" 48 + 49 + shiki@^0.14.1: 50 + version "0.14.3" 51 + resolved "https://registry.yarnpkg.com/shiki/-/shiki-0.14.3.tgz#d1a93c463942bdafb9866d74d619a4347d0bbf64" 52 + integrity sha512-U3S/a+b0KS+UkTyMjoNojvTgrBHjgp7L6ovhFVZsXmBGnVdQ4K4U9oK0z63w538S91ATngv1vXigHCSWOwnr+g== 53 + dependencies: 54 + ansi-sequence-parser "^1.1.0" 55 + jsonc-parser "^3.2.0" 56 + vscode-oniguruma "^1.7.0" 57 + vscode-textmate "^8.0.0" 58 + 59 + typedoc@^0.23.28: 60 + version "0.23.28" 61 + resolved "https://registry.yarnpkg.com/typedoc/-/typedoc-0.23.28.tgz#3ce9c36ef1c273fa849d2dea18651855100d3ccd" 62 + integrity sha512-9x1+hZWTHEQcGoP7qFmlo4unUoVJLB0H/8vfO/7wqTnZxg4kPuji9y3uRzEu0ZKez63OJAUmiGhUrtukC6Uj3w== 63 + dependencies: 64 + lunr "^2.3.9" 65 + marked "^4.2.12" 66 + minimatch "^7.1.3" 67 + shiki "^0.14.1" 68 + 69 + typescript@^4.9.5: 70 + version "4.9.5" 71 + resolved "https://registry.yarnpkg.com/typescript/-/typescript-4.9.5.tgz#095979f9bcc0d09da324d58d03ce8f8374cbe65a" 72 + integrity sha512-1FXk9E2Hm+QzZQ7z+McJiHL4NW1F2EzMu9Nq9i3zAaGqibafqYwCVU6WyWAuyQRRzOlxou8xZSyXLEN8oKj24g== 73 + 74 + vscode-oniguruma@^1.7.0: 75 + version "1.7.0" 76 + resolved "https://registry.yarnpkg.com/vscode-oniguruma/-/vscode-oniguruma-1.7.0.tgz#439bfad8fe71abd7798338d1cd3dc53a8beea94b" 77 + integrity sha512-L9WMGRfrjOhgHSdOYgCt/yRMsXzLDJSL7BPrOZt73gU0iWO4mpqzqQzOz5srxqTvMBaR0XZTSrVWo4j55Rc6cA== 78 + 79 + vscode-textmate@^8.0.0: 80 + version "8.0.0" 81 + resolved "https://registry.yarnpkg.com/vscode-textmate/-/vscode-textmate-8.0.0.tgz#2c7a3b1163ef0441097e0b5d6389cd5504b59e5d" 82 + integrity sha512-AFbieoL7a5LMqcnOF04ji+rpXadgOXnZsxQr//r83kLPr7biP7am3g9zbaZIaBGwBRWeSvoMD4mgPdX3e4NWBg==