Skip to content
Open
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
43 changes: 31 additions & 12 deletions index.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import {
startCasify,
upperCasify,
lowerCasify,
snakeUpperCasify,
} from './dist';

// Test utils
Expand All @@ -30,16 +31,14 @@ function generateArray() {
];
}


function prependBlueCase(input: string) {
return `blue_${input}`;
}


it('camelCasify - handles objects', () => {
const obj = {};
const fn = () => {};
const re = /casify/ig;
const re = /casify/gi;

const input = generateObject(obj, fn, re);

Expand All @@ -61,7 +60,7 @@ it('camelCasify - handles objects', () => {
it('bumpyCasify - handles objects', () => {
const obj = {};
const fn = () => {};
const re = /casify/ig;
const re = /casify/gi;

const input = generateObject(obj, fn, re);

Expand All @@ -83,7 +82,7 @@ it('bumpyCasify - handles objects', () => {
it('kebabCasify - handles objects', () => {
const obj = {};
const fn = () => {};
const re = /casify/ig;
const re = /casify/gi;

const input = generateObject(obj, fn, re);

Expand All @@ -105,7 +104,7 @@ it('kebabCasify - handles objects', () => {
it('snakeCasify - handles objects', () => {
const obj = {};
const fn = () => {};
const re = /casify/ig;
const re = /casify/gi;

const input = generateObject(obj, fn, re);

Expand All @@ -126,7 +125,7 @@ it('snakeCasify - handles objects', () => {
it('startCasify - handles objects', () => {
const obj = {};
const fn = () => {};
const re = /casify/ig;
const re = /casify/gi;

const input = generateObject(obj, fn, re);

Expand All @@ -147,7 +146,7 @@ it('startCasify - handles objects', () => {
it('upperCasify - handles objects', () => {
const obj = {};
const fn = () => {};
const re = /casify/ig;
const re = /casify/gi;

const input = generateObject(obj, fn, re);

Expand All @@ -168,7 +167,7 @@ it('upperCasify - handles objects', () => {
it('lowerCasify - handles objects', () => {
const obj = {};
const fn = () => {};
const re = /casify/ig;
const re = /casify/gi;

const input = generateObject(obj, fn, re);

Expand All @@ -186,10 +185,32 @@ it('lowerCasify - handles objects', () => {
expect(expected).toEqual(observed);
});

it('snakeUpperCasify - handles objects', () => {
const obj = {};
const fn = () => {};
const re = /casify/gi;

const input = generateObject(obj, fn, re);

const expected = {
SNAKE_CASE: true,
KEBAB_CASE: 1000,
CAMEL_CASE: 'camelCase',
BUMPY_CASE: 11.99,
START_CASE: obj,
UPPER_CASE: fn,
LOWER_CASE: re,
};

const observed = snakeUpperCasify(input);

expect(expected).toEqual(observed);
});

it('casify - handles objects', () => {
const obj = {};
const fn = () => {};
const re = /casify/ig;
const re = /casify/gi;

const input = generateObject(obj, fn, re);

Expand All @@ -207,7 +228,6 @@ it('casify - handles objects', () => {
expect(expected).toEqual(observed);
});


it('camelCasify - handles arrays', () => {
const input = generateArray();

Expand All @@ -220,4 +240,3 @@ it('camelCasify - handles arrays', () => {
const observed = camelCasify(input);
expect(expected).toEqual(observed);
});

9 changes: 7 additions & 2 deletions index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,15 +8,15 @@ import uppercase from 'lodash.uppercase';
import lowercase from 'lodash.lowercase';
import upperfirst from 'lodash.upperfirst';

type Collection = Record<any, any>
type Collection = Record<any, any>;
type CaseFn = (input: string) => string;

function casifyObject<T>(caseFn: CaseFn, obj: Collection): Collection {
return mapKeys(obj, (value, key) => caseFn(key));
}

function casifyArray<T>(caseFn: CaseFn, array: Array<T>): Array<T> {
return map(array, obj => casifyObject(caseFn, obj));
return map(array, (obj) => casifyObject(caseFn, obj));
}

function casify(caseFn: CaseFn, collection: Collection) {
Expand All @@ -32,12 +32,17 @@ function bumpyCase(input: string) {
return upperfirst(camelCase(input));
}

function snakeUpperCase(input: string) {
return snakeCase(input).toUpperCase();
}

export const camelCasify = casifyFnFactory(camelCase);
export const bumpyCasify = casifyFnFactory(bumpyCase);
export const kebabCasify = casifyFnFactory(kebabCase);
export const snakeCasify = casifyFnFactory(snakeCase);
export const startCasify = casifyFnFactory(startCase);
export const upperCasify = casifyFnFactory(uppercase);
export const lowerCasify = casifyFnFactory(lowercase);
export const snakeUpperCasify = casifyFnFactory(snakeUpperCase);

export default casify;
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,7 @@
},
"license": "MIT",
"devDependencies": {
"@types/jest": "^27.0.2",
"@types/lodash.camelcase": "^4.3.6",
"@types/lodash.kebabcase": "^4.1.6",
"@types/lodash.lowercase": "^4.3.6",
Expand Down