From ae77e5216b0da196dbeb3c0dde442a3dc0caae9b Mon Sep 17 00:00:00 2001 From: Denis Date: Mon, 5 Jun 2017 19:32:27 -0400 Subject: [PATCH] Implemented humanize-plus definition (#16977) * Implemented humanize-plus definition * Update package.json * Fix linting issue --- types/humanize-plus/humanize-plus-tests.ts | 204 +++++++++++++++++++++ types/humanize-plus/index.d.ts | 30 +++ types/humanize-plus/tsconfig.json | 22 +++ types/humanize-plus/tslint.json | 1 + 4 files changed, 257 insertions(+) create mode 100644 types/humanize-plus/humanize-plus-tests.ts create mode 100644 types/humanize-plus/index.d.ts create mode 100644 types/humanize-plus/tsconfig.json create mode 100644 types/humanize-plus/tslint.json diff --git a/types/humanize-plus/humanize-plus-tests.ts b/types/humanize-plus/humanize-plus-tests.ts new file mode 100644 index 0000000000..4e66ff6c26 --- /dev/null +++ b/types/humanize-plus/humanize-plus-tests.ts @@ -0,0 +1,204 @@ +import * as Humanize from 'humanize-plus'; + +///////////// +// Numbers // +///////////// + +/** + * formatNumber + */ +Humanize.formatNumber(123456789, 2); +// "123,456,789.00" + +/** + * intComma + */ +Humanize.intComma(123456789); +// "123,456,789" + +/** + * compactInteger + */ +Humanize.compactInteger(123456789, 1); +// "123.5M" + +// Switch to scientific notation for trillons, because no one knows those abbreviations. +Humanize.compactInteger(-7832186132456328967, 4); +// "-7.8322x10^18" + +Humanize.compactInteger(-100, 2); +// "-100" + +/** + * boundedNumber + */ +Humanize.boundedNumber(110, 100); +// "100+" + +Humanize.boundedNumber(50, 100); +// "50" + +/** + * ordinal + */ +Humanize.ordinal(22); +// "22nd" + +/** + * times + */ +for (let i = 0; i < 5; i ++) { + Humanize.times(i, {4: 'too many'}); + if (i === 1) { + Humanize.times(1.1); + } +} +// never +// once +// 1.1 times +// twice +// 3 times +// too many times + +/** + * pace + */ +const second = 1000; +const week = 6.048e8; +const decade = 3.156e11; + +Humanize.pace(1.5, second, "heartbeat"); +// Approximately 2 heartbeats per second + +Humanize.pace(4, week); +// Approximately 4 times per week + +Humanize.pace(1, decade, "life crisis"); +// Less than 1 life crisis per week + +/** + * fileSize + */ +Humanize.fileSize(1024 * 20); +// "20 Kb" + +Humanize.fileSize(1024 * 2000); +// "1.95 Mb" + +Humanize.fileSize(Math.pow(1000, 4)); +// "931.32 Gb" + +/** + * pluralize + */ +Humanize.pluralize(1, "duck"); +// "duck" + +Humanize.pluralize(3, "duck"); +// "ducks" + +Humanize.pluralize(3, "duck", "duckies"); +// "duckies" + +///////////// +// Strings // +///////////// + +/** + * truncate + */ +Humanize.truncate('long text is good for you'); +// "long text is good for you" + +Humanize.truncate('long text is good for you', 19); +// "long text is goo..." + +Humanize.truncate('long text is good for you', 19, '... etc'); +// "long text is... etc" + +/** + * truncateWords + */ +Humanize.truncateWords('long text is good for you', 5); +// "long text is good for ..." + +/** + * capitalize + */ +Humanize.capitalize("some boring string"); +// "Some boring string" + +Humanize.capitalize("wHoOaA!"); +// "WHoOaA!" + +Humanize.capitalize("wHoOaA!", true); +// "Whooaa!" + +/** + * capitalizeAll + */ +Humanize.capitalizeAll("some boring string"); +// "Some Boring String" + +/** + * titleCase + */ +Humanize.titleCase("some of a boring string"); +// "Some of a Boring String" + +Humanize.titleCase("cool the iTunes cake, O'Malley!"); +// "Cool the iTunes Cake, O'Malley!" + +//////////// +// Arrays // +//////////// + +/** + * oxford + */ +const items = ['apple', 'orange', 'banana', 'pear', 'pineapple']; + +Humanize.oxford(items); +// "apple, orange, banana, pear, and pineapple" + +Humanize.oxford(items, 3); +// "apple, orange, banana, and 2 others" + +// Pluralizes properly too! +Humanize.oxford(items, 4); +// "apple, orange, banana, pear, and 1 other" + +Humanize.oxford(items, 3, "and some other fruits"); +// "apple, orange, banana, and some other fruits" + +/** + * frequency + */ +const aznPics = [ + 'http://24.media.tumblr.com/77082543cb69af56ede38a0cdb2511d0/tumblr_mh96olWPLv1r8k4ywo1_1280.jpg', + 'http://25.media.tumblr.com/3e2d318be34d5ef8f86a612cd1d795ff/tumblr_mhbhb96t3z1r8k4ywo1_1280.jpg', + 'http://24.media.tumblr.com/8c5a052e33c27c784514e1b124b383a1/tumblr_mhexaqrk0w1r8k4ywo1_1280.jpg' +]; +const bigfootPics: string[] = []; + +"Instagrammers " + Humanize.frequency(aznPics, "took pictures of food"); +// "Instagrammers took pictures of food 3 times" + +"Bigfoot " + Humanize.frequency(bigfootPics, "took pictures of food"); +// "Bigfoot never took pictures of food" + +///////////////////// +// Utility methods // +///////////////////// + +/** + * toFixed + */ +Humanize.toFixed(0.615, 2); +// "0.62" + +/** + * normalizePrecision + */ +Humanize.normalizePrecision(-232.231); +// 232 diff --git a/types/humanize-plus/index.d.ts b/types/humanize-plus/index.d.ts new file mode 100644 index 0000000000..4b20ada3fd --- /dev/null +++ b/types/humanize-plus/index.d.ts @@ -0,0 +1,30 @@ +// Type definitions for humanize-plus 1.8 +// Project: https://github.com/HubSpot/humanize +// Definitions by: Denis Carriere +// Definitions: https://github.com/DefinitelyTyped/DefinitelyTyped + +// Numbers +export function formatNumber(number: number, decimals?: number): string; +export function intComma(number: number, decimals?: number): string; +export function compactInteger(number: number, decimals?: number): string; +export function boundedNumber(number: number, bounds?: number, ending?: string): string; +export function ordinal(value: number): string; +export function times(value: number, overrides?: any): string; +export function pace(value: number, intervalMs: number, unit?: string): string; +export function fileSize(filesize: number, precision?: number): string; +export function pluralize(number: number, singular?: string, plural?: string): string; + +// Strings +export function truncate(string: string, length?: number, ending?: string): string; +export function truncateWords(string: string, length?: number): string | null; +export function capitalize(string: string, downCaseTail?: boolean): string; +export function capitalizeAll(string: string): string; +export function titleCase(string: string): string; + +// Arrays +export function oxford(items: any[], limit?: number, limitStr?: string): string; +export function frequency(list: any[], verb?: string): string; + +// Utility methods +export function toFixed(value: number, precision?: number): string; +export function normalizePrecision(value: number, base?: number): number; diff --git a/types/humanize-plus/tsconfig.json b/types/humanize-plus/tsconfig.json new file mode 100644 index 0000000000..c79c7259f7 --- /dev/null +++ b/types/humanize-plus/tsconfig.json @@ -0,0 +1,22 @@ +{ + "compilerOptions": { + "module": "commonjs", + "lib": [ + "es6" + ], + "noImplicitAny": true, + "noImplicitThis": true, + "strictNullChecks": true, + "baseUrl": "../", + "typeRoots": [ + "../" + ], + "types": [], + "noEmit": true, + "forceConsistentCasingInFileNames": true + }, + "files": [ + "index.d.ts", + "humanize-plus-tests.ts" + ] +} diff --git a/types/humanize-plus/tslint.json b/types/humanize-plus/tslint.json new file mode 100644 index 0000000000..3db14f85ea --- /dev/null +++ b/types/humanize-plus/tslint.json @@ -0,0 +1 @@ +{ "extends": "dtslint/dt.json" }