Skip to content

Commit

Permalink
feat: added formatNumber - alias to numberFormat. Updated docs
Browse files Browse the repository at this point in the history
  • Loading branch information
syntaxlexx committed Aug 22, 2023
1 parent 204912b commit 301c37f
Show file tree
Hide file tree
Showing 4 changed files with 47 additions and 8 deletions.
22 changes: 16 additions & 6 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand All @@ -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()
- insertIntoArray()


## Dev Notes
### Testing
```bash
npm t
```

### Publish to NPM
```bash
npm publish --access public
```
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -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",
Expand Down
21 changes: 21 additions & 0 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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)
}
10 changes: 9 additions & 1 deletion src/utils.test.ts
Original file line number Diff line number Diff line change
@@ -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 =============*/
Expand Down Expand Up @@ -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("");
Expand Down

0 comments on commit 301c37f

Please sign in to comment.