@agape/string
Advanced tools
Comparing version 1.0.3 to 1.1.0
@@ -34,2 +34,11 @@ /** | ||
/** | ||
* Format a number in units, pluralizing the units if there is more or less than | ||
* one count. | ||
* @param count Number of units | ||
* @param unit Label for the value | ||
* @param plural Set to false to disable pluralization | ||
* @returns String in `x units` format | ||
*/ | ||
export declare function quanitfy(count: number | string, unit: string, pluralize?: boolean): string; | ||
/** | ||
* Formats a string in it's plural form. Most strings a returned | ||
@@ -36,0 +45,0 @@ * with an 's' appended to the end. For strings that end with 'y', |
"use strict"; | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
exports.titalize = exports.pluralize = exports.classify = exports.verbalize = exports.tokenize = exports.camelize = void 0; | ||
exports.titalize = exports.quanitfy = exports.pluralize = exports.classify = exports.verbalize = exports.tokenize = exports.camelize = void 0; | ||
/** | ||
@@ -41,4 +41,3 @@ * Returns a string formatted in camel case. | ||
return string | ||
// split words on capital letters | ||
.replace(/(.)([A-Z][a-z]+)/g, (str, left, right) => { return left + ' ' + right.toLowerCase(); }) | ||
.replace(/(.)([A-Z][a-z]+)/g, (str, left, right) => { return left + ' ' + right; }) | ||
.replace(/[-_]/, ' ') | ||
@@ -82,3 +81,20 @@ .replace(/^([a-z])/, (str) => { return str.toUpperCase(); }); | ||
exports.pluralize = pluralize; | ||
// define a file-level copy of the pluralize function so that the quanitfy | ||
// function can have a `pluralize` attribute and still use `_pluralize` | ||
const _pluralize = pluralize; | ||
/** | ||
* Format a number in units, pluralizing the units if there is more or less than | ||
* one count. | ||
* @param count Number of units | ||
* @param unit Label for the value | ||
* @param plural Set to false to disable pluralization | ||
* @returns String in `x units` format | ||
*/ | ||
function quanitfy(count, unit, pluralize = true) { | ||
const value = typeof count == 'number' ? count : Number(count); | ||
const label = pluralize === false || value === 1 ? unit : _pluralize(unit); | ||
return `${count} ${label}`; | ||
} | ||
exports.quanitfy = quanitfy; | ||
/** | ||
* Formats a string in it's plural form. Most strings a returned | ||
@@ -85,0 +101,0 @@ * with an 's' appended to the end. For strings that end with 'y', |
@@ -34,2 +34,11 @@ /** | ||
/** | ||
* Format a number in units, pluralizing the units if there is more or less than | ||
* one count. | ||
* @param count Number of units | ||
* @param unit Label for the value | ||
* @param plural Set to false to disable pluralization | ||
* @returns String in `x units` format | ||
*/ | ||
export declare function quanitfy(count: number | string, unit: string, pluralize?: boolean): string; | ||
/** | ||
* Formats a string in it's plural form. Most strings a returned | ||
@@ -36,0 +45,0 @@ * with an 's' appended to the end. For strings that end with 'y', |
"use strict"; | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
exports.titalize = exports.pluralize = exports.classify = exports.verbalize = exports.tokenize = exports.camelize = void 0; | ||
exports.titalize = exports.quanitfy = exports.pluralize = exports.classify = exports.verbalize = exports.tokenize = exports.camelize = void 0; | ||
/** | ||
@@ -41,4 +41,3 @@ * Returns a string formatted in camel case. | ||
return string | ||
// split words on capital letters | ||
.replace(/(.)([A-Z][a-z]+)/g, (str, left, right) => { return left + ' ' + right.toLowerCase(); }) | ||
.replace(/(.)([A-Z][a-z]+)/g, (str, left, right) => { return left + ' ' + right; }) | ||
.replace(/[-_]/, ' ') | ||
@@ -82,3 +81,20 @@ .replace(/^([a-z])/, (str) => { return str.toUpperCase(); }); | ||
exports.pluralize = pluralize; | ||
// define a file-level copy of the pluralize function so that the quanitfy | ||
// function can have a `pluralize` attribute and still use `_pluralize` | ||
const _pluralize = pluralize; | ||
/** | ||
* Format a number in units, pluralizing the units if there is more or less than | ||
* one count. | ||
* @param count Number of units | ||
* @param unit Label for the value | ||
* @param plural Set to false to disable pluralization | ||
* @returns String in `x units` format | ||
*/ | ||
function quanitfy(count, unit, pluralize = true) { | ||
const value = typeof count == 'number' ? count : Number(count); | ||
const label = pluralize === false || value === 1 ? unit : _pluralize(unit); | ||
return `${count} ${label}`; | ||
} | ||
exports.quanitfy = quanitfy; | ||
/** | ||
* Formats a string in it's plural form. Most strings a returned | ||
@@ -85,0 +101,0 @@ * with an 's' appended to the end. For strings that end with 'y', |
{ | ||
"name": "@agape/string", | ||
"version": "1.0.3", | ||
"version": "1.1.0", | ||
"description": "String and token manipulation", | ||
@@ -5,0 +5,0 @@ "main": "dist/cjs/public_api.js", |
import {} from "jasmine"; | ||
import { camelize, tokenize, verbalize, classify, pluralize, titalize } from './string' | ||
import { camelize, tokenize, verbalize, classify, pluralize, titalize, quanitfy } from './string' | ||
@@ -82,9 +82,57 @@ | ||
it('should verbalize the string', () => { | ||
expect( verbalize('fooBar') ).toEqual('Foo bar') | ||
}) | ||
describe('quantify', () => { | ||
let value: number|string; | ||
let label: string; | ||
beforeEach( () => { | ||
value = undefined; | ||
label = 'cat'; | ||
}) | ||
it('should verbalize the string', () => { | ||
expect( verbalize('firstName') ).toEqual('First name') | ||
it('should pluralize the label if < 0', () => { | ||
value = -1; | ||
expect( quanitfy(value, label) ).toBe("-1 cats") | ||
}) | ||
it('should pluralize the label if 0', () => { | ||
value = 0; | ||
expect( quanitfy(value, label) ).toBe("0 cats") | ||
}) | ||
it('should pluralize the label if > 0 and < 1', () => { | ||
value = 0.5; | ||
expect( quanitfy(value, label) ).toBe("0.5 cats") | ||
}) | ||
it('should not pluralize the label if 1', () => { | ||
value = 1; | ||
expect( quanitfy(value, label) ).toBe("1 cat") | ||
}) | ||
it('should pluralize the label if 1.x', () => { | ||
value = -1; | ||
expect( quanitfy(value, label) ).toBe("-1 cats") | ||
}) | ||
it('should pluralize the label if >= 2', () => { | ||
value = 2; | ||
expect( quanitfy(value, label) ).toBe("2 cats") | ||
value = 420; | ||
expect( quanitfy(value, label) ).toBe("420 cats") | ||
}) | ||
it('should display formatted numbers', () => { | ||
value = "05.6"; | ||
expect( quanitfy(value, label) ).toBe("05.6 cats") | ||
}) | ||
it('should display formatted numbers', () => { | ||
value = "432.00"; | ||
expect( quanitfy(value, label) ).toBe("432.00 cats") | ||
}) | ||
}) |
@@ -46,4 +46,3 @@ | ||
return string | ||
// split words on capital letters | ||
.replace(/(.)([A-Z][a-z]+)/g , (str, left, right) => { return left + ' ' + right.toLowerCase() } ) | ||
.replace(/(.)([A-Z][a-z]+)/g , (str, left, right) => { return left + ' ' + right } ) | ||
.replace(/[-_]/, ' ') | ||
@@ -96,4 +95,25 @@ .replace(/^([a-z])/, (str) => { return str.toUpperCase() } ) | ||
// define a file-level copy of the pluralize function so that the quanitfy | ||
// function can have a `pluralize` attribute and still use `_pluralize` | ||
const _pluralize = pluralize; | ||
/** | ||
* Format a number in units, pluralizing the units if there is more or less than | ||
* one count. | ||
* @param count Number of units | ||
* @param unit Label for the value | ||
* @param plural Set to false to disable pluralization | ||
* @returns String in `x units` format | ||
*/ | ||
export function quanitfy( count: number|string, unit: string, pluralize = true ) { | ||
const value = typeof count == 'number' ? count : Number(count); | ||
const label = pluralize === false || value === 1 ? unit : _pluralize(unit); | ||
return `${count} ${label}` | ||
} | ||
/** | ||
* Formats a string in it's plural form. Most strings a returned | ||
@@ -100,0 +120,0 @@ * with an 's' appended to the end. For strings that end with 'y', |
@@ -14,6 +14,7 @@ { | ||
"esModuleInterop": true, | ||
"forceConsistentCasingInFileNames": true | ||
"forceConsistentCasingInFileNames": true, | ||
"strictNullChecks": false, | ||
}, | ||
"include": ["src/**/*", "index.ts", ], | ||
"exclude": ["node_modules", "**/__tests__/*", "**/*.spec.ts"] | ||
"exclude": ["node_modules", "**/__tests__/*"] | ||
} |
Sorry, the diff of this file is not supported yet
Copyleft License
License(Experimental) Copyleft license information was found
Found 1 instance in 1 package
No License Found
License(Experimental) License information could not be found
Found 1 instance in 1 package
Non-permissive License
License(Experimental) A license not known to be considered permissive was found
Found 1 instance in 1 package
36683
22
0
818
13
1
2
1
3