Skip to content

Commit

Permalink
feat(currency): new feature
Browse files Browse the repository at this point in the history
BREAKING CHANGE: #33
  • Loading branch information
yussan committed Apr 5, 2018
1 parent ff44f79 commit 04be643
Show file tree
Hide file tree
Showing 6 changed files with 1,021 additions and 80 deletions.
11 changes: 10 additions & 1 deletion docs/modules.md
Original file line number Diff line number Diff line change
Expand Up @@ -84,4 +84,13 @@
import {toSlug} from 'string-manager'
const slug = 'iam ready (for everything) to start'
```
will be **'iam-ready-for-everything-to-start'**
will be **'iam-ready-for-everything-to-start'**

## Currency

- `currencyFormat(int, str)`, set dot after three digit for currency nominal.
```
import { currencyFormat } from 'string-manager
const price = currencyFormat(245000, 'Rp')
```
will be **'Rp 245.000'**
61 changes: 32 additions & 29 deletions src/index.js
Original file line number Diff line number Diff line change
@@ -1,35 +1,38 @@
import * as _httpquery from './modules/httpquery'
import * as _camelcase from './modules/camelcase'
import * as _html from './modules/html'
import * as _spaces from './modules/spaces'
import * as _truncate from './modules/truncate'
import * as _slug from './modules/slug'
// import * as _httpquery from './modules/httpquery'
// import * as _camelcase from './modules/camelcase'
// import * as _html from './modules/html'
// import * as _spaces from './modules/spaces'
// import * as _truncate from './modules/truncate'
// import * as _slug from './modules/slug'
// import * as _currency from './modules/currency'

function StringManager(str) {
this.data = str
return this.data
}
// function StringManager(str) {
// this.data = str
// return this.data
// }

function prototypeBuilder(module) {
return function(...args) {
return new StringManager(module(this.data, ...args))
}
}
// function prototypeBuilder(module) {
// return function(...args) {
// return new StringManager(module(this.data, ...args))
// }
// }

StringManager.prototype.toString = (...args) => { return this.data }
StringManager.prototype.objToQuery = prototypeBuilder(_httpquery.objToQuery)
StringManager.prototype.queryToObj = prototypeBuilder(_httpquery.queryToObj)
StringManager.prototype.toCamelCase = prototypeBuilder(_camelcase.toCamelCase)
StringManager.prototype.stripTags = prototypeBuilder(_html.stripTags)
StringManager.prototype.toSingleSpace = prototypeBuilder(_spaces.toSingleSpace)
// StringManager.prototype.toString = (...args) => { return this.data }
// StringManager.prototype.objToQuery = prototypeBuilder(_httpquery.objToQuery)
// StringManager.prototype.queryToObj = prototypeBuilder(_httpquery.queryToObj)
// StringManager.prototype.toCamelCase = prototypeBuilder(_camelcase.toCamelCase)
// StringManager.prototype.stripTags = prototypeBuilder(_html.stripTags)
// StringManager.prototype.toSingleSpace = prototypeBuilder(_spaces.toSingleSpace)
// StringManager.prototype.truncate = prototypeBuilder(_truncate.truncate)
StringManager.prototype.toSlug = prototypeBuilder(_slug.toSlug)
// StringManager.prototype.toSlug = prototypeBuilder(_slug.toSlug)
// StringManager.prototype.currencyFormat = prototypeBuilder(_currency.currencyFormat)

export {toCamelCase} from './modules/camelcase'
export {objToQuery, queryToObj} from './modules/httpquery'
export {stripTags} from './modules/html'
export {toSingleSpace} from './modules/spaces'
export {truncate} from './modules/truncate'
export {toSlug} from './modules/slug'
export { toCamelCase } from './modules/camelcase'
export { objToQuery, queryToObj } from './modules/httpquery'
export { stripTags } from './modules/html'
export { toSingleSpace } from './modules/spaces'
export { truncate } from './modules/truncate'
export { toSlug } from './modules/slug'
export { currencyFormat } from './modules/currency'

export default str => { return new StringManager(str)}
// export default str => { return new StringManager(str)}
9 changes: 9 additions & 0 deletions src/modules/currency.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
export function currencyFormat(number, symbol ='') {
try {
// convert number to string
number = number.toString()
return `${symbol ? `${symbol} ` : ''}${number.replace(/(\d)(?=(\d\d\d)+(?!\d))/g, "$1.")}`
} catch(e) {
console.error(e)
}
}
16 changes: 16 additions & 0 deletions test/currency.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
import {expect} from 'chai'
import {currencyFormat} from '../src/index'

describe("Currency Module", () => {
describe('currencyFormat()', () => {
it("normal use", () => {
const res = currencyFormat(456679, 'Rp')
expect(res).to.deep.equal('Rp 456.679')
})
it("format not number", () => {
const res = currencyFormat('string', '')
expect(res).to.deep.equal('string')
})
})
})
// })
21 changes: 0 additions & 21 deletions test/index.js

This file was deleted.

Loading

0 comments on commit 04be643

Please sign in to comment.