Skip to content

Commit

Permalink
Add title casing string function (#156)
Browse files Browse the repository at this point in the history
* add title casing string function

Co-authored-by: Ray Epps <rayepps@vessel.local>
  • Loading branch information
sodiray and Ray Epps authored Nov 15, 2022
1 parent 6952a98 commit 965c33e
Show file tree
Hide file tree
Showing 8 changed files with 58 additions and 11 deletions.
2 changes: 1 addition & 1 deletion cdn/.version
Original file line number Diff line number Diff line change
@@ -1 +1 @@
9.2.0
9.3.0
7 changes: 6 additions & 1 deletion cdn/radash.esm.js
Original file line number Diff line number Diff line change
Expand Up @@ -755,10 +755,15 @@ const pascal = (str) => {
return "";
return parts.map((str2) => str2.charAt(0).toUpperCase() + str2.slice(1)).join("");
};
const title = (str) => {
if (!str)
return "";
return str.split(/(?=[A-Z])|[\.\-\s_]/).map((s) => s.trim()).filter((s) => !!s).map((s) => capitalize(s.toLowerCase())).join(" ");
};
const template = (str, data, regex = /\{\{(.+?)\}\}/g) => {
return Array.from(str.matchAll(regex)).reduce((acc, match) => {
return acc.replace(match[0], data[match[1]]);
}, str);
};

export { alphabetical, boil, camel as camal, camel, capitalize, chain, clone, cluster, compose, counting, dash, debounce, defer, diff, draw, first, flat, fork, get, group, intersects, invert, isArray, isDate, isEmpty, isEqual, isFloat, isFunction, isInt, isNumber, isObject, isPrimitive, isString, isSymbol, iterate, last, list, listify, lowerize, map, mapEntries, mapKeys, mapValues, max, memo, merge, min, objectify, omit, parallel, partial, partob, pascal, pick, proxied, random, range, reduce, replace, replaceOrAppend, retry, select, series, shake, shift, shuffle, sift, sleep, snake, sort, sum, template, throttle, toFloat, toInt, toggle, tryit as try, tryit, uid, unique, upperize, zip };
export { alphabetical, boil, camel as camal, camel, capitalize, chain, clone, cluster, compose, counting, dash, debounce, defer, diff, draw, first, flat, fork, get, group, intersects, invert, isArray, isDate, isEmpty, isEqual, isFloat, isFunction, isInt, isNumber, isObject, isPrimitive, isString, isSymbol, iterate, last, list, listify, lowerize, map, mapEntries, mapKeys, mapValues, max, memo, merge, min, objectify, omit, parallel, partial, partob, pascal, pick, proxied, random, range, reduce, replace, replaceOrAppend, retry, select, series, shake, shift, shuffle, sift, sleep, snake, sort, sum, template, throttle, title, toFloat, toInt, toggle, tryit as try, tryit, uid, unique, upperize, zip };
6 changes: 6 additions & 0 deletions cdn/radash.js
Original file line number Diff line number Diff line change
Expand Up @@ -758,6 +758,11 @@ var radash = (function (exports) {
return "";
return parts.map((str2) => str2.charAt(0).toUpperCase() + str2.slice(1)).join("");
};
const title = (str) => {
if (!str)
return "";
return str.split(/(?=[A-Z])|[\.\-\s_]/).map((s) => s.trim()).filter((s) => !!s).map((s) => capitalize(s.toLowerCase())).join(" ");
};
const template = (str, data, regex = /\{\{(.+?)\}\}/g) => {
return Array.from(str.matchAll(regex)).reduce((acc, match) => {
return acc.replace(match[0], data[match[1]]);
Expand Down Expand Up @@ -837,6 +842,7 @@ var radash = (function (exports) {
exports.sum = sum;
exports.template = template;
exports.throttle = throttle;
exports.title = title;
exports.toFloat = toFloat;
exports.toInt = toInt;
exports.toggle = toggle;
Expand Down
2 changes: 1 addition & 1 deletion cdn/radash.min.js

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "radash",
"version": "9.2.0",
"version": "9.3.0",
"description": "Functional utility library - modern, simple, typed, powerful",
"main": "dist/cjs/index.cjs",
"module": "dist/esm/index.mjs",
Expand Down
3 changes: 2 additions & 1 deletion src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,8 @@ export {
dash,
pascal,
snake,
template
template,
title
} from './string'
export {
isArray,
Expand Down
26 changes: 22 additions & 4 deletions src/string.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ export const capitalize = (str: string): string => {
}

/**
* Joins all the words of the string in a camel case fashion
* Formats the given string in camel case fashion
*
* camel('hello world') -> 'helloWorld'
* camel('va va-VOOM') -> 'vaVaVoom'
Expand All @@ -31,7 +31,7 @@ export const camel = (str: string): string => {
}

/**
* Joins all the words of the string in a snake case fashion
* Formats the given string in snake case fashion
*
* snake('hello world') -> 'hello_world'
* snake('va va-VOOM') -> 'va_va_voom'
Expand All @@ -51,7 +51,7 @@ export const snake = (str: string): string => {
}

/**
* Joins all the words of the string in a dash case fashion
* Formats the given string in dash case fashion
*
* dash('hello world') -> 'hello-world'
* dash('va va_VOOM') -> 'va-va-voom'
Expand All @@ -71,7 +71,7 @@ export const dash = (str: string): string => {
}

/**
* Joins all string arguments in a Pascal case fashion
* Formats the given string in pascal case fashion
*
* pascal('hello world') -> 'HelloWorld'
* pascal('va va boom') -> 'VaVaBoom'
Expand All @@ -82,6 +82,24 @@ export const pascal = (str: string): string => {
return parts.map(str => str.charAt(0).toUpperCase() + str.slice(1)).join('')
}

/**
* Formats the given string in title case fashion
*
* title('hello world') -> 'Hello World'
* title('va_va_boom') -> 'Va Va Boom'
* title('root-hook') -> 'Root Hook'
* title('queryItems') -> 'Query Items'
*/
export const title = (str: string | null | undefined): string => {
if (!str) return ''
return str
.split(/(?=[A-Z])|[\.\-\s_]/)
.map(s => s.trim())
.filter(s => !!s)
.map(s => capitalize(s.toLowerCase()))
.join(' ')
}

/**
* template is used to replace data by name in template strings.
* The default expression looks for {{name}} to identify names.
Expand Down
21 changes: 19 additions & 2 deletions src/tests/string.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -143,8 +143,8 @@ describe('string module', () => {
})
})

describe('PascalCase function', () => {
test('returns non alphanumerics in PascalCase', () => {
describe('pascal function', () => {
test('returns non alphanumerics in pascal', () => {
const result = _.pascal('Exobase Starter_flash AND-go')
assert.equal(result, 'ExobaseStarterFlashAndGo')
})
Expand All @@ -157,4 +157,21 @@ describe('string module', () => {
assert.equal(result, '')
})
})

describe('title function', () => {
test('returns input formatted in title case', () => {
assert.equal(_.title('hello world'), 'Hello World')
assert.equal(_.title('va_va_boom'), 'Va Va Boom')
assert.equal(_.title('root-hook - ok!'), 'Root Hook Ok!')
assert.equal(_.title('queryItems'), 'Query Items')
assert.equal(
_.title('queryAllItems-in_Database'),
'Query All Items In Database'
)
})
test('returns empty string for bad input', () => {
assert.equal(_.title(null), '')
assert.equal(_.title(undefined), '')
})
})
})

0 comments on commit 965c33e

Please sign in to comment.