From 301c37fd2e358fd738cd7b12cb04a3e99735c773 Mon Sep 17 00:00:00 2001 From: SyntaxLexx Date: Tue, 22 Aug 2023 21:07:31 +0300 Subject: [PATCH] feat: added formatNumber - alias to numberFormat. Updated docs --- README.md | 22 ++++++++++++++++------ package.json | 2 +- src/index.ts | 21 +++++++++++++++++++++ src/utils.test.ts | 10 +++++++++- 4 files changed, 47 insertions(+), 8 deletions(-) diff --git a/README.md b/README.md index 53eb5b7..be807d9 100644 --- a/README.md +++ b/README.md @@ -9,13 +9,10 @@ npm i @acelords/js-utils pnpm i @acelords/js-utils ``` - -```bash -npm publish --access public -``` +` ## Docs -Available functions. [View the entire list here](./src/index.ts) +Available functions. [View the entire list here](https://github.com/acelords/node-utils/blob/main/src/index.ts) - formatDateTime() - getTimeFromDate() - randomNumber() @@ -24,10 +21,23 @@ Available functions. [View the entire list here](./src/index.ts) - ucwords() - substring() - numberFormat() +- formatNumber() - formatCurrency() - slugify() - stripTags() - plural() - singular() - pluralize() -- insertIntoArray() \ No newline at end of file +- insertIntoArray() + + +## Dev Notes +### Testing +```bash +npm t +``` + +### Publish to NPM +```bash +npm publish --access public +``` diff --git a/package.json b/package.json index d1f90ce..5fe7d85 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "@acelords/js-utils", - "version": "1.0.4", + "version": "1.0.5", "description": "Common utils and helpers used on node projects", "main": "dist/index.js", "types": "dist/index.d.ts", diff --git a/src/index.ts b/src/index.ts index d97d524..2857b34 100644 --- a/src/index.ts +++ b/src/index.ts @@ -106,6 +106,17 @@ export const numberFormat = (value: string | number | undefined | null, toInt = return numeral(value).format(format) } +/** + * An Alias for numberFormat + * format a number to 2dp. + * - 1000 becomes 1,000.00. + * - If toInt=true, 1000000 becomes 1,000,000. + * - Displaying other groupings/separators is possible, look at the docs http://numeraljs.com/ + */ +export const formatNumber = (value: string | number | undefined | null, toInt = false): string => { + return numberFormat(value, toInt) +} + /** * format currency. Value passed must be in cents. * - 1500000 becomes 15,000.00 @@ -196,3 +207,13 @@ export const insertIntoArray = (arr, index, ...newItems) => [ // part of the array after the specified index ...arr.slice(index) ] + + +/** + * SQL month starts from 1-12, js starts from 0-11 + * @param index Month index as returned from SQL + * @returns string dayjs().format() + */ +export function getMonthNameFromSqlMonthIndex(index: number, format = 'MMM') { + return dayjs(new Date(2023, index - 1, 1)).format(format) +} diff --git a/src/utils.test.ts b/src/utils.test.ts index e521b86..92d8848 100644 --- a/src/utils.test.ts +++ b/src/utils.test.ts @@ -1,4 +1,4 @@ -import { formatCurrency, formatDate, formatDateTime, fromNow, getTimeFromDate, insertIntoArray, isNumeric, numberFormat, plural, pluralize, randomNumber, singular, slugify, stripTags, substring, ucwords } from './index' +import { formatCurrency, formatDate, formatDateTime, formatNumber, fromNow, getTimeFromDate, insertIntoArray, isNumeric, numberFormat, plural, pluralize, randomNumber, singular, slugify, stripTags, substring, ucwords } from './index' import dayjs from 'dayjs'; /*======== formatDate =============*/ @@ -151,6 +151,14 @@ test("numberFormat - can format numeric strings and numbers", () => { expect(numberFormat('123456', true)).toBe("123,456"); }); +test("formatNumber - can format numeric strings and numbers", () => { + expect(formatNumber('abcd')).toBe(""); + expect(formatNumber(null)).toBe(""); + expect(formatNumber(undefined)).toBe(""); + expect(formatNumber('123456')).toBe("123,456.00"); + expect(formatNumber('123456', true)).toBe("123,456"); +}); + /*======== formatCurrency =============*/ test("formatCurrency - can format numeric strings and numbers to currency", () => { expect(formatCurrency('abcd')).toBe("");