Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

currency-formatter

Package Overview
Dependencies
Maintainers
1
Versions
40
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

currency-formatter - npm Package Compare versions

Comparing version 1.0.4 to 1.0.5

50

index.js

@@ -41,2 +41,37 @@ var currencies = require('./currencies')

var formatMapping = [
{
symbolOnLeft: true,
spaceBetweenAmountAndSymbol: false,
format: {
pos: '%s%v',
neg: '-%s%v'
}
},
{
symbolOnLeft: true,
spaceBetweenAmountAndSymbol: true,
format: {
pos: '%s %v',
neg: '-%s %v'
}
},
{
symbolOnLeft: false,
spaceBetweenAmountAndSymbol: false,
format: {
pos: '%v%s',
neg: '-%v%s'
}
},
{
symbolOnLeft: false,
spaceBetweenAmountAndSymbol: true,
format: {
pos: '%v %s',
neg: '-%v %s'
}
}
]
exports.format = function (value, options) {

@@ -48,12 +83,5 @@ var currency = findCurrency(options.code) || exports.defaultCurrency

var format = ''
if (symbolOnLeft) {
format = spaceBetweenAmountAndSymbol
? '%s %v'
: '%s%v'
} else {
format = spaceBetweenAmountAndSymbol
? '%v %s'
: '%v%s'
}
var format = formatMapping.find(function(f) {
return f.symbolOnLeft == symbolOnLeft && f.spaceBetweenAmountAndSymbol == spaceBetweenAmountAndSymbol
}).format

@@ -77,3 +105,3 @@ return accounting.formatMoney(value, {

format: typeof options.format === 'string'
format: ['string', 'object'].indexOf(typeof options.format) > -1
? options.format

@@ -80,0 +108,0 @@ : format

{
"name": "currency-formatter",
"version": "1.0.4",
"version": "1.0.5",
"description": "A simple Javascript utility that helps you to display currency properly",

@@ -5,0 +5,0 @@ "main": "index.js",

@@ -52,3 +52,2 @@ # Currency Formatter

```JAVASCRIPT
var currencyFormatter = require('currency-formatter');

@@ -64,2 +63,12 @@ currencyFormatter.format(1000000, {

// => '1^000^000*0 @'
// Different formatting for positive and negative values
currencyFormatter.format(-10, {
format: {
pos: '%s%v' // %s is the symbol and %v is the value
neg: '(%s%v)'
}
});
// => ($10)
```

@@ -66,0 +75,0 @@

@@ -9,2 +9,7 @@ var chai = require('chai')

context('No Space', () => {
it('Returns -$10.00 for -10', () => {
var result = currencyFormatter.format(-10, { code: 'USD' })
assert.equal(result, '-$10.00')
})
it('Returns $10.00 for 10', () => {

@@ -37,2 +42,7 @@ var result = currencyFormatter.format(10, { code: 'USD' })

context('With Space', () => {
it('Returns -$ 10,00 for -10', () => {
var result = currencyFormatter.format(-10, { code: 'ARS' })
assert.equal(result, '-$ 10,00')
})
it('Returns $ 10,00 for 10', () => {

@@ -67,2 +77,7 @@ var result = currencyFormatter.format(10, { code: 'ARS' })

context('No Space', () => {
it('Returns -10.00Nfk for -10', () => {
var result = currencyFormatter.format(-10, { code: 'ERN' })
assert.equal(result, '-10.00Nfk')
})
it('Returns 10.00Nfk for 10', () => {

@@ -95,2 +110,7 @@ var result = currencyFormatter.format(10, { code: 'ERN' })

context('With Space', () => {
it('Returns -10,00 € for -10', () => {
var result = currencyFormatter.format(-10, { code: 'EUR' })
assert.equal(result, '-10,00 €')
})
it('Returns 10,00 € for 10', () => {

@@ -138,2 +158,26 @@ var result = currencyFormatter.format(10, { code: 'EUR' })

it('Supports objects for format, to override the positive result', () => {
var result = currencyFormatter.format(10, {
code: 'USD',
format: {
pos: '%s %v',
neg: '-%s%v'
}
})
assert.equal(result, '$ 10.00')
})
it('Supports objects for format, to override the negative result', () => {
var result = currencyFormatter.format(-10, {
code: 'USD',
format: {
pos: '%s %v',
neg: '(%s%v)'
}
})
assert.equal(result, '($10.00)')
})
it('Supports empty symbol', () => {

@@ -140,0 +184,0 @@ var result = currencyFormatter.format(1000000, {

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc