From 9b910ede10cdcbefd95362a2e9196997fd6c8eae Mon Sep 17 00:00:00 2001 From: Orlando Date: Fri, 13 Oct 2023 11:06:41 +0100 Subject: [PATCH] scrambleText --- CHANGELOG.md | 6 ++++++ README.md | 3 +++ package.json | 2 +- src/helpers/index.ts | 1 + src/helpers/scrambleText.test.ts | 9 +++++++++ src/helpers/scrambleText.ts | 8 ++++++++ src/random/index.ts | 4 +++- src/random/randomChar.ts | 5 +++++ src/random/randomPassword.test.ts | 5 +++-- src/random/randomPassword.ts | 14 +++++++++----- src/random/randomString.test.ts | 9 +++++++++ src/random/randomString.ts | 10 ++++++++++ src/regex/letterRegex.ts | 1 + 13 files changed, 68 insertions(+), 9 deletions(-) create mode 100644 src/helpers/scrambleText.test.ts create mode 100644 src/helpers/scrambleText.ts create mode 100644 src/random/randomChar.ts create mode 100644 src/random/randomString.test.ts create mode 100644 src/random/randomString.ts create mode 100644 src/regex/letterRegex.ts diff --git a/CHANGELOG.md b/CHANGELOG.md index dc3b1cb..a514a74 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,5 +1,11 @@ # deverything +## 0.32.0 + +### Minor Changes + +- scrambler + ## 0.31.0 ### Minor Changes diff --git a/README.md b/README.md index 326f554..a5736e5 100644 --- a/README.md +++ b/README.md @@ -78,6 +78,7 @@ Contributions always welcome! - ⭐ `parseDate()` pass anything Date-Like, and get a JS Date back - `pretty()` stringify anything, without breaking on circular dependencies - `promiseWithTimeout()` takes a promise, a timeoutMs, and an option error as arguments. Returns a new Promise that either resolves with the value of the input promise or rejects with the provided error or a default error message if the input promise does not resolve or reject within the specified timeoutMs. +- `scrambleText()` replace alpha chars with random chars - `sleep()` promise-based sleep - `shuffle()` shuffles elements in an array - `toggleArrayValue()` remove/add value in array @@ -93,6 +94,7 @@ These functions are optimized for low entropy random data generation useful for - ⭐ `randomArrayItem()` - `randomBankAccount()` - `randomBool()` +- `randomChar()` - `randomCompany()` - ⭐ `randomCoords()` - `randomLat()` @@ -128,6 +130,7 @@ These functions are optimized for low entropy random data generation useful for - `randomParagraph()` - `randomPassword()` - `randomPhoneNumber()` +- `randomString()` - `randomUUID()` lightweight uuid generation, passing UUID validation - `randomWord()` diff --git a/package.json b/package.json index bdb3373..1278c2b 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "deverything", - "version": "0.31.0", + "version": "0.32.0", "description": "Everything you need for Dev", "main": "./dist/index.js", "module": "./dist/index.mjs", diff --git a/src/helpers/index.ts b/src/helpers/index.ts index ed1acc2..950032d 100644 --- a/src/helpers/index.ts +++ b/src/helpers/index.ts @@ -19,6 +19,7 @@ export * from "./omit"; export * from "./parseDate"; export * from "./pretty"; export * from "./promiseWithTimeout"; +export * from "./scrambleText"; export * from "./serialize"; export * from "./shuffle"; export * from "./sleep"; diff --git a/src/helpers/scrambleText.test.ts b/src/helpers/scrambleText.test.ts new file mode 100644 index 0000000..96cd244 --- /dev/null +++ b/src/helpers/scrambleText.test.ts @@ -0,0 +1,9 @@ +import { expect, test } from "@jest/globals"; +import { scrambleText } from "./scrambleText"; + +test("scrambleText", async () => { + const string = "Hello World, \n how are we?"; + const scrambled = scrambleText(string); + expect(scrambled).toHaveLength(26); + expect(scrambled).not.toBe(string); +}); diff --git a/src/helpers/scrambleText.ts b/src/helpers/scrambleText.ts new file mode 100644 index 0000000..ec29858 --- /dev/null +++ b/src/helpers/scrambleText.ts @@ -0,0 +1,8 @@ +import { randomChar } from "../random/randomChar"; +import { letterRegex } from "../regex/letterRegex"; + +export const scrambleText = (str: string): string => { + return str.replace(letterRegex, () => { + return randomChar(); + }); +}; diff --git a/src/random/index.ts b/src/random/index.ts index f568783..f37ae4f 100644 --- a/src/random/index.ts +++ b/src/random/index.ts @@ -3,6 +3,7 @@ export * from "./randomAlphaNumericCode"; export * from "./randomArrayItem"; export * from "./randomBankAccount"; export * from "./randomBool"; +export * from "./randomChar"; export * from "./randomCompany"; export * from "./randomCoords"; export * from "./randomDate"; @@ -22,8 +23,9 @@ export * from "./randomIP"; export * from "./randomName"; export * from "./randomNumericCode"; export * from "./randomNumericId"; -export * from "./randomPhoneNumber"; export * from "./randomParagraph"; export * from "./randomPassword"; +export * from "./randomPhoneNumber"; +export * from "./randomString"; export * from "./randomUUID"; export * from "./randomWord"; diff --git a/src/random/randomChar.ts b/src/random/randomChar.ts new file mode 100644 index 0000000..b7f835b --- /dev/null +++ b/src/random/randomChar.ts @@ -0,0 +1,5 @@ +import { randomInt } from "./randomInt"; + +export const randomChar = () => { + return String.fromCharCode(randomInt(97, 122)); +}; diff --git a/src/random/randomPassword.test.ts b/src/random/randomPassword.test.ts index 2cf57c5..e549085 100644 --- a/src/random/randomPassword.test.ts +++ b/src/random/randomPassword.test.ts @@ -3,7 +3,8 @@ import { randomPassword } from "./randomPassword"; describe(`randomPassword`, () => { it(`no args`, () => { - expect(randomPassword().length).toBeGreaterThan(9); - expect(randomPassword({ minChars: 19 }).length).toBeGreaterThan(19); + expect(randomPassword().length).toBeGreaterThan(1); + expect(randomPassword({ minChars: 19 }).length).toBeGreaterThanOrEqual(19); + expect(randomPassword({ maxChars: 20 }).length).toBeLessThanOrEqual(20); }); }); diff --git a/src/random/randomPassword.ts b/src/random/randomPassword.ts index eaf29b8..1af3c54 100644 --- a/src/random/randomPassword.ts +++ b/src/random/randomPassword.ts @@ -1,9 +1,13 @@ import { SPECIAL_CHARACTERS } from "../constants/unicode"; import { randomArrayItem } from "./randomArrayItem"; -import { randomHtmlColorName } from "./randomHtmlColorName"; import { randomInt } from "./randomInt"; +import { randomString } from "./randomString"; -export const randomPassword = ({ minChars = 9 }: { minChars?: number } = {}) => - randomHtmlColorName().padEnd(minChars, "-") + // So it has an upper case, at least 9 charss - randomArrayItem(SPECIAL_CHARACTERS) + // So it has a special character - randomInt(11, 99); // So it has a number +export const randomPassword = ({ + minChars = 9, + maxChars = 32, +}: { minChars?: number; maxChars?: number } = {}) => + randomString({ length: 1 }).toUpperCase() + // Upper case + randomString({ length: randomInt(minChars, maxChars) - 3 }) + // At least 9 chars + randomArrayItem(SPECIAL_CHARACTERS) + // Special character + randomInt(1, 9); // Number diff --git a/src/random/randomString.test.ts b/src/random/randomString.test.ts new file mode 100644 index 0000000..9f53b10 --- /dev/null +++ b/src/random/randomString.test.ts @@ -0,0 +1,9 @@ +import { describe, it, expect } from "@jest/globals"; +import { randomString } from "./randomString"; + +describe(`randomString`, () => { + it(`no args`, () => { + expect(randomString().length).toBe(10); + expect(randomString({ length: 19 }).length).toBe(19); + }); +}); diff --git a/src/random/randomString.ts b/src/random/randomString.ts new file mode 100644 index 0000000..c82903b --- /dev/null +++ b/src/random/randomString.ts @@ -0,0 +1,10 @@ +import { array } from "../helpers"; +import { randomChar } from "./randomChar"; + +export const randomString = ({ + length = 10, +}: { + length?: number; +} = {}) => { + return array(length, () => randomChar()).join(""); +}; diff --git a/src/regex/letterRegex.ts b/src/regex/letterRegex.ts new file mode 100644 index 0000000..f35f75e --- /dev/null +++ b/src/regex/letterRegex.ts @@ -0,0 +1 @@ +export const letterRegex = new RegExp(/\p{L}/, "gu");