Skip to content
This repository has been archived by the owner on May 20, 2023. It is now read-only.

Add @codinasion/tools #6

Draft
wants to merge 1 commit into
base: master
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,9 @@
"lint": "turbo run lint",
"clean": "turbo run clean",
"format": "prettier --write \"**/*.{js,jsx,ts,tsx,md,mdx,json}\"",
"all": "pnpm run clean && pnpm run lint && pnpm run build:local && pnpm run format"
"test": "turbo run test",
"test:watch": "turbo run test:watch",
"all": "pnpm run clean && pnpm run lint && pnpm run build:local && pnpm run test && pnpm run format"
},
"devDependencies": {
"dotenv-cli": "latest",
Expand Down
1 change: 1 addition & 0 deletions packages/shared/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
{
"name": "shared",
"version": "0.0.0",
"private": true,
"sideEffects": [
"**/*.css"
],
Expand Down
4 changes: 4 additions & 0 deletions packages/tools/.eslintrc.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
module.exports = {
root: true,
extends: ["custom"],
};
7 changes: 7 additions & 0 deletions packages/tools/jestconfig.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
{
"transform": {
"^.+\\.(t|j)sx?$": "ts-jest"
},
"testRegex": "(/__test__/.*|(\\.|/)(test|spec))\\.(jsx?|tsx?)$",
"moduleFileExtensions": ["ts", "tsx", "js", "jsx", "json", "node"]
}
29 changes: 29 additions & 0 deletions packages/tools/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
{
"name": "@codinasion/tools",
"version": "0.0.0",
"main": "./dist/index.js",
"module": "./dist/index.mjs",
"types": "./dist/index.d.ts",
"files": [
"dist"
],
"license": "MIT",
"scripts": {
"lint": "eslint \"**/*.{ts,tsx}*\"",
"build": "tsup",
"dev": "tsup --watch",
"check-types": "tsc --noEmit",
"test": "jest --config jestconfig.json --maxWorkers=50%",
"test:watch": "jest --watch"
},
"devDependencies": {
"@types/jest": "^29.5.1",
"eslint": "^7.32.0",
"eslint-config-custom": "workspace:*",
"jest": "^29.5.0",
"tsconfig": "workspace:*",
"tsup": "^6.1.3",
"typescript": "^4.5.2",
"ts-jest": "^29.1.0"
}
}
11 changes: 11 additions & 0 deletions packages/tools/src/convert-binary-to-decimal/__test__/test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
import ConvertBinaryToDecimal from "../index";

describe("ConvertBinaryToDecimal", () => {
it("should return 0 when input is 0", () => {
expect(ConvertBinaryToDecimal("0")).toBe(0);
});

it("should return 9 when input is 1001", () => {
expect(ConvertBinaryToDecimal("1001")).toBe(9);
});
});
6 changes: 6 additions & 0 deletions packages/tools/src/convert-binary-to-decimal/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
export default function ConvertBinaryToDecimal(binary: string): number {
const binaryArray = binary.split("").reverse();
return binaryArray.reduce((acc, curr, index) => {
return acc + Number(curr) * Math.pow(2, index);
}, 0);
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
import ConvertBinaryToHexadecimal from "../index";

describe("ConvertBinaryToHexadecimal", () => {
it("should return 0 when input is 0", () => {
expect(ConvertBinaryToHexadecimal("0")).toBe("0");
});

it("should return 9 when input is 1001", () => {
expect(ConvertBinaryToHexadecimal("1001")).toBe("9");
});
});
3 changes: 3 additions & 0 deletions packages/tools/src/convert-binary-to-hexadecimal/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
export default function ConvertBinaryToHexadecimal(binary: string): string {
return parseInt(binary, 2).toString(16);
}
8 changes: 8 additions & 0 deletions packages/tools/src/convert-binary-to-octal/__test__/test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
import ConvertBinaryToOctal from "../index";

describe("ConvertBinaryToOctal", () => {
it("should convert binary to octal", () => {
expect(ConvertBinaryToOctal("1000")).toEqual(10);
expect(ConvertBinaryToOctal("100000")).toEqual(40);
});
});
6 changes: 6 additions & 0 deletions packages/tools/src/convert-binary-to-octal/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
import ConvertDecimalToOctal from "../convert-decimal-to-octal";
import ConvertBinaryToDecimal from "../convert-binary-to-decimal";

export default function ConvertBinaryToOctal(binary: string): number {
return ConvertDecimalToOctal(ConvertBinaryToDecimal(binary));
}
11 changes: 11 additions & 0 deletions packages/tools/src/convert-decimal-to-binary/__test__/test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
import convertDecimalToBinary from "../index";

describe("convertDecimalToBinary", () => {
it("should return 0 when input is 0", () => {
expect(convertDecimalToBinary(0)).toBe("0");
});

it("should return 1001 when input is 9", () => {
expect(convertDecimalToBinary(9)).toBe("1001");
});
});
3 changes: 3 additions & 0 deletions packages/tools/src/convert-decimal-to-binary/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
export default function ConvertDecimalToBinary(decimal: number): string {
return decimal.toString(2);
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
import ConvertDecimalToHexadecimal from "../index";

describe("ConvertDecimalToHexadecimal", () => {
it("should convert decimal to hexadecimal", () => {
expect(ConvertDecimalToHexadecimal(11)).toBe("b");
expect(ConvertDecimalToHexadecimal(31)).toBe("1f");
});
});
16 changes: 16 additions & 0 deletions packages/tools/src/convert-decimal-to-hexadecimal/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
export default function ConvertDecimalToHexadecimal(decimal: number): string {
if (decimal === 0) {
return "0";
}
const hexArray = [];
while (decimal > 0) {
const remainder = decimal % 16;
if (remainder < 10) {
hexArray.push(remainder);
} else {
hexArray.push(String.fromCharCode(remainder + 55).toLowerCase());
}
decimal = Math.floor(decimal / 16);
}
return hexArray.reverse().join("");
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
import ConvertDecimalToOctal from "../index";

describe("ConvertDecimalToOctal", () => {
it("should convert decimal to octal", () => {
expect(ConvertDecimalToOctal(10)).toEqual(12);
expect(ConvertDecimalToOctal(40)).toEqual(50);
});
});
5 changes: 5 additions & 0 deletions packages/tools/src/convert-decimal-to-octal/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
export default function ConvertDecimalToOctal(decimal: number): number {
const octalStr = decimal.toString(8);
const octal = parseInt(octalStr, 10);
return octal;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
import ConvertHexadecimalToBinary from "../index";

describe("ConvertHexadecimalToBinary", () => {
it("should convert hexadecimal to binary", () => {
expect(ConvertHexadecimalToBinary("1")).toBe("0001");
expect(ConvertHexadecimalToBinary("E")).toBe("1110");
});
});
12 changes: 12 additions & 0 deletions packages/tools/src/convert-hexadecimal-to-binary/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
export default function convertHexadecimalToBinary(
hexadecimal: string
): string {
const binary = hexadecimal
.split("")
.map((hexadecimalDigit) => {
const binaryDigit = parseInt(hexadecimalDigit, 16).toString(2);
return binaryDigit.padStart(4, "0");
})
.join("");
return binary;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
import ConvertHexadecimalToDecimal from "../index";

describe("ConvertHexadecimalToDecimal", () => {
it("should convert hexadecimal to decimal", () => {
expect(ConvertHexadecimalToDecimal("a")).toBe(10);
expect(ConvertHexadecimalToDecimal("ff")).toBe(255);
});
});
5 changes: 5 additions & 0 deletions packages/tools/src/convert-hexadecimal-to-decimal/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
export default function ConvertHexadecimalToDecimal(
hexadecimal: string
): number {
return parseInt(hexadecimal, 16);
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
import ConvertHexadecimalToOctal from "../index";

describe("ConvertHexadecimalToOctal", () => {
it("should convert hexadecimal to octal", () => {
expect(ConvertHexadecimalToOctal("a")).toBe(12);
expect(ConvertHexadecimalToOctal("1f")).toBe(37);
});
});
6 changes: 6 additions & 0 deletions packages/tools/src/convert-hexadecimal-to-octal/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
import ConvertHexadecimalToDecimal from "../convert-hexadecimal-to-decimal";
import ConvertDecimalToOctal from "../convert-decimal-to-octal";

export default function ConvertHexadecimalToOctal(hexadecimal: string): number {
return ConvertDecimalToOctal(ConvertHexadecimalToDecimal(hexadecimal));
}
11 changes: 11 additions & 0 deletions packages/tools/src/convert-octal-to-binary/__test__/test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
import ConvertOctalToBinary from "../index";

describe("ConvertOctalToBinary", () => {
it("should return 111 when input is 7", () => {
expect(ConvertOctalToBinary(7)).toBe("111");
});

it("should return 1011 when input is 13", () => {
expect(ConvertOctalToBinary(13)).toBe("1011");
});
});
6 changes: 6 additions & 0 deletions packages/tools/src/convert-octal-to-binary/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
import ConvertOctalToDecimal from "../convert-octal-to-decimal";
import ConvertDecimalToBinary from "../convert-decimal-to-binary";

export default function ConvertOctalToBinary(octal: number): string {
return ConvertDecimalToBinary(ConvertOctalToDecimal(octal));
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
import ConvertOctalToDecimal from "../index";

describe("ConvertOctalToDecimal", () => {
it("should convert octal to decimal", () => {
expect(ConvertOctalToDecimal(10)).toBe(8);
expect(ConvertOctalToDecimal(20)).toBe(16);
});
});
13 changes: 13 additions & 0 deletions packages/tools/src/convert-octal-to-decimal/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
export default function ConvertOctalToDecimal(octalString: number): number {
let octal = octalString;
let decimal = 0;
let i = 0;

while (octal !== 0) {
decimal += (octal % 10) * Math.pow(8, i);
++i;
octal = Math.floor(octal / 10);
}

return decimal;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
import ConvertOctalToHexadecimal from "../index";

describe("ConvertOctalToHexadecimal", () => {
it("should return 0 when input is 0", () => {
expect(ConvertOctalToHexadecimal(0)).toBe("0");
});

it("should return 1f when input is 37", () => {
expect(ConvertOctalToHexadecimal(37)).toBe("1f");
});
});
6 changes: 6 additions & 0 deletions packages/tools/src/convert-octal-to-hexadecimal/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
import ConvertOctalToDecimal from "../convert-octal-to-decimal";
import ConvertDecimalToHexadecimal from "../convert-decimal-to-hexadecimal";

export default function ConvertOctalToHexadecimal(octal: number): string {
return ConvertDecimalToHexadecimal(ConvertOctalToDecimal(octal));
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
import FormatStringToCamelCase from "../index";

test("FormatStringToCamelCase", () => {
expect(FormatStringToCamelCase("foo-bar")).toBe("fooBar");
expect(FormatStringToCamelCase("foo bar_baz-foo bar-baz")).toBe(
"fooBarBazFooBarBaz"
);
});
11 changes: 11 additions & 0 deletions packages/tools/src/format-string-to-camelcase/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
export default function FormatStringToCamelCase(str: string): string {
return str
.split(/[-_ ]/)
.map((word, index) => {
if (index === 0) {
return word;
}
return word.charAt(0).toUpperCase() + word.slice(1);
})
.join("");
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
import FormatStringToConstantCase from "../index";

test("FormatStringToConstantCase", () => {
expect(FormatStringToConstantCase("foo-bar")).toBe("FOO_BAR");
expect(FormatStringToConstantCase("foo bar_baz-foo bar-baz")).toBe(
"FOO_BAR_BAZ_FOO_BAR_BAZ"
);
});
6 changes: 6 additions & 0 deletions packages/tools/src/format-string-to-constantcase/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
export default function FormatStringToConstantCase(str: string): string {
return str
.split(/[-_ ]+/)
.map((word) => word.toUpperCase())
.join("_");
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
import FormatStringToDotcase from "../index";

test("FormatStringToDotcase", () => {
expect(FormatStringToDotcase("foo-bar")).toBe("foo.bar");
expect(FormatStringToDotcase("foo bar_baz-foo bar-baz")).toBe(
"foo.bar.baz.foo.bar.baz"
);
});
3 changes: 3 additions & 0 deletions packages/tools/src/format-string-to-dotcase/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
export default function FormatStringToDotCase(str: string): string {
return str.split(/[-_ ]/).join(".");
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
import FormatStringToKebabCase from "../index";

test("FormatStringToKebabCase", () => {
expect(FormatStringToKebabCase("foo-bar")).toBe("foo-bar");
expect(FormatStringToKebabCase("foo bar_baz-foo bar-baz")).toBe(
"foo-bar-baz-foo-bar-baz"
);
});
3 changes: 3 additions & 0 deletions packages/tools/src/format-string-to-kebabcase/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
export default function FormatStringToKebabCase(str: string): string {
return str.split(/[-_ ]/).join("-");
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
import FormatStringToPascalCase from "../index";

test("FormatToPascalCase", () => {
expect(FormatStringToPascalCase("foo-bar")).toBe("FooBar");
expect(FormatStringToPascalCase("foo bar_baz-foo bar-baz")).toBe(
"FooBarBazFooBarBaz"
);
});
8 changes: 8 additions & 0 deletions packages/tools/src/format-string-to-pascalcase/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
export default function FormatStringToPascalCase(input: string): string {
return input
.split(/[-_ ]/)
.map((word) => {
return word.charAt(0).toUpperCase() + word.slice(1);
})
.join("");
}
10 changes: 10 additions & 0 deletions packages/tools/src/format-string-to-pathcase/__test__/test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
import FormatStringToPathCase from "../index";

test("FormatStringToPathCase", () => {
expect(FormatStringToPathCase("foo bar-baz_qux quux")).toBe(
"foo/bar/baz/qux/quux"
);
expect(FormatStringToPathCase("foo bar-baz_qux_sdf-quux")).toBe(
"foo/bar/baz/qux/sdf/quux"
);
});
8 changes: 8 additions & 0 deletions packages/tools/src/format-string-to-pathcase/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
export default function FormatStringToPathCase(input: string): string {
return input
.split(/[-_ ]/)
.map((word) => {
return word.toLowerCase();
})
.join("/");
}
10 changes: 10 additions & 0 deletions packages/tools/src/format-string-to-snakecase/__test__/test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
import FormatStringToSnakecase from "../index";

test("FormatStringToSnakecase", () => {
expect(FormatStringToSnakecase("foo bar-baz_qux quux")).toBe(
"foo_bar_baz_qux_quux"
);
expect(FormatStringToSnakecase("foo bar-baz_qux_sdf-quux")).toBe(
"foo_bar_baz_qux_sdf_quux"
);
});
Loading