Skip to content

Commit

Permalink
scrambleText
Browse files Browse the repository at this point in the history
  • Loading branch information
ogroppo committed Oct 13, 2023
1 parent e16af15 commit 9b910ed
Show file tree
Hide file tree
Showing 13 changed files with 68 additions and 9 deletions.
6 changes: 6 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,11 @@
# deverything

## 0.32.0

### Minor Changes

- scrambler

## 0.31.0

### Minor Changes
Expand Down
3 changes: 3 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -93,6 +94,7 @@ These functions are optimized for low entropy random data generation useful for
-`randomArrayItem()`
- `randomBankAccount()`
- `randomBool()`
- `randomChar()`
- `randomCompany()`
-`randomCoords()`
- `randomLat()`
Expand Down Expand Up @@ -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()`

Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -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",
Expand Down
1 change: 1 addition & 0 deletions src/helpers/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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";
Expand Down
9 changes: 9 additions & 0 deletions src/helpers/scrambleText.test.ts
Original file line number Diff line number Diff line change
@@ -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);
});
8 changes: 8 additions & 0 deletions src/helpers/scrambleText.ts
Original file line number Diff line number Diff line change
@@ -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();
});
};
4 changes: 3 additions & 1 deletion src/random/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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";
Expand All @@ -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";
5 changes: 5 additions & 0 deletions src/random/randomChar.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
import { randomInt } from "./randomInt";

export const randomChar = () => {
return String.fromCharCode(randomInt(97, 122));
};
5 changes: 3 additions & 2 deletions src/random/randomPassword.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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);
});
});
14 changes: 9 additions & 5 deletions src/random/randomPassword.ts
Original file line number Diff line number Diff line change
@@ -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
9 changes: 9 additions & 0 deletions src/random/randomString.test.ts
Original file line number Diff line number Diff line change
@@ -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);
});
});
10 changes: 10 additions & 0 deletions src/random/randomString.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
import { array } from "../helpers";
import { randomChar } from "./randomChar";

export const randomString = ({
length = 10,
}: {
length?: number;
} = {}) => {
return array(length, () => randomChar()).join("");
};
1 change: 1 addition & 0 deletions src/regex/letterRegex.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export const letterRegex = new RegExp(/\p{L}/, "gu");

0 comments on commit 9b910ed

Please sign in to comment.