Skip to content

Commit

Permalink
feat: add path case (#14)
Browse files Browse the repository at this point in the history
  • Loading branch information
imevanc authored Sep 8, 2024
1 parent d0fda5f commit 90acdf1
Show file tree
Hide file tree
Showing 4 changed files with 110 additions and 3 deletions.
9 changes: 7 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
- 🦀 **ConstantCase** - Get your string into `CONSTANT_CASE`.
- 🐸 **NoCase** - Convert any string to `no case`.
- 🐬 **SnakeCase** - Format string in `snake_case`.
- 🦁 **PathCase** - Get your string into `/pathCase`.

---

Expand Down Expand Up @@ -50,7 +51,8 @@ import {
toConstantCase,
toDotCase,
toNoCase,
toSnakeCase
toSnakeCase,
toPathCase
} from 'casenator';

// Camel Case
Expand Down Expand Up @@ -84,7 +86,10 @@ console.log(toConstantCase('hello world')); // 'HELLO_WORLD'
console.log(toNoCase('hello--world')); // 'hello world'

// Snake Case
console.log(toSnakeCase('hello world')); // 'hello_world'
console.log(toSnakeCase('hello world')); // 'hello_world'\

// Path Case
console.log(toPathCase('hello world')); // '/helloWorld'
```

---
Expand Down
60 changes: 60 additions & 0 deletions __test__/index.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import {
toLowerCase,
toNoCase,
toPascalCase,
toPathCase,
toSnakeCase,
toUpperCase,
} from "../src";
Expand Down Expand Up @@ -378,3 +379,62 @@ describe("toSnakeCase", () => {
);
});
});

describe("toPathCase", () => {
// Valid Cases
test("should convert HeLLo_world-123! to /helloWorld123", () => {
expect(toPathCase("HeLLo_world-123!")).toBe("/helloWorld123");
});

test("should convert hello 123 world to /helloWorld123", () => {
expect(toPathCase("hello 123 world")).toBe("/hello123World");
});

test("should convert hello-world to /helloWorld", () => {
expect(toPathCase("hello-world")).toBe("/helloWorld");
});

test("should convert space-separated string to /camelCase", () => {
expect(toPathCase("hello world")).toBe("/helloWorld");
});

test("should convert underscore-separated string to /camelCase", () => {
expect(toPathCase("hello_world")).toBe("/helloWorld");
});

test("should convert mixed-case string with symbols to /camelCase", () => {
expect(toPathCase("HeLLo_WoRld-123!")).toBe("/helloWorld123");
});

test("should convert a single word to camelCase with a leading slash", () => {
expect(toPathCase("HELLO")).toBe("/hello");
});

// Edge Cases
test("should return empty string for empty input", () => {
expect(toPathCase("")).toBe("/");
});

test("should handle string with only special characters", () => {
expect(toPathCase("!@#$%^&*")).toBe("/");
});

test("should handle string with multiple spaces", () => {
expect(toPathCase(" hello world ")).toBe("/helloWorld");
});

// Error Cases
test("should throw TypeError when input is not a string", () => {
expect(() => toPathCase(123)).toThrow(TypeError);
expect(() => toPathCase(null)).toThrow(TypeError);
expect(() => toPathCase(undefined)).toThrow(TypeError);
expect(() => toPathCase({})).toThrow(TypeError);
expect(() => toPathCase([])).toThrow(TypeError);
});

test("should throw TypeError with the correct message when input is not a string", () => {
expect(() => toPathCase(123)).toThrow("Input must be a string");
expect(() => toPathCase([])).toThrow("Input must be a string");
expect(() => toPathCase({})).toThrow("Input must be a string");
});
});
21 changes: 20 additions & 1 deletion dist/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
Object.defineProperty(exports, "__esModule", {
value: true
});
exports.toUpperCase = exports.toSnakeCase = exports.toPascalCase = exports.toNoCase = exports.toLowerCase = exports.toKebabCase = exports.toDotCase = exports.toConstantCase = exports.toCapitalCase = exports.toCamelCase = exports.substring = exports.reverseString = void 0;
exports.toUpperCase = exports.toSnakeCase = exports.toPathCase = exports.toPascalCase = exports.toNoCase = exports.toLowerCase = exports.toKebabCase = exports.toDotCase = exports.toConstantCase = exports.toCapitalCase = exports.toCamelCase = exports.substring = exports.reverseString = void 0;
/**
* Converts a string to camelCase.
*/
Expand Down Expand Up @@ -138,4 +138,23 @@ var toSnakeCase = exports.toSnakeCase = function toSnakeCase(input) {
.replace(/_+/g, "_") // Replace multiple consecutive underscores with a single underscore
.replace(/(^_|_$)/g, "") // Remove leading or trailing underscores
.toLowerCase(); // Convert the entire string to lowercase
};

/**
* Converts a string to /pathCase
*/
var toPathCase = exports.toPathCase = function toPathCase(input) {
if (typeof input !== "string") throw new TypeError("Input must be a string");
return "/" + input.trim() // Remove leading/trailing spaces
.replace(/[^a-zA-Z0-9]+/g, " ") // Replace non-alphanumeric characters with spaces
.replace(/\s+/g, " ") // Normalize multiple spaces to a single space
.split(" ") // Split by spaces into words
.map(function (word, index) {
if (index === 0) {
return word.toLowerCase(); // First word stays lowercase
}
return word.charAt(0).toUpperCase() + word.slice(1).toLowerCase(); // Capitalize the first letter of subsequent words
}).join("") // Join back into a single string
.replace(/[^a-zA-Z0-9]/g, "") // Remove any remaining non-alphanumeric characters
;
};
23 changes: 23 additions & 0 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -131,3 +131,26 @@ export const toSnakeCase = (input) => {
.replace(/(^_|_$)/g, "") // Remove leading or trailing underscores
.toLowerCase(); // Convert the entire string to lowercase
};

/**
* Converts a string to /pathCase
*/
export const toPathCase = (input) => {
if (typeof input !== "string") throw new TypeError("Input must be a string");
return (
"/" +
input
.trim() // Remove leading/trailing spaces
.replace(/[^a-zA-Z0-9]+/g, " ") // Replace non-alphanumeric characters with spaces
.replace(/\s+/g, " ") // Normalize multiple spaces to a single space
.split(" ") // Split by spaces into words
.map((word, index) => {
if (index === 0) {
return word.toLowerCase(); // First word stays lowercase
}
return word.charAt(0).toUpperCase() + word.slice(1).toLowerCase(); // Capitalize the first letter of subsequent words
})
.join("") // Join back into a single string
.replace(/[^a-zA-Z0-9]/g, "") // Remove any remaining non-alphanumeric characters
);
};

0 comments on commit 90acdf1

Please sign in to comment.