Coder Social home page Coder Social logo

currency-symbol-map's Introduction

currency-symbol-map

A function to lookup the currency symbol for a given currency code.

Installation

npm install currency-symbol-map

Usage

Get symbol from currency code

// ES5
const getSymbolFromCurrency = require('currency-symbol-map')

// ES6
import getSymbolFromCurrency from 'currency-symbol-map'

getSymbolFromCurrency('GBP') //=> '£'
getSymbolFromCurrency('EUR') //=> '€'
getSymbolFromCurrency('USD') //=> '$'
getSymbolFromCurrency('NOT A VALID CODE') //=> undefined

Exposed map for other processing

// ES5
const currencyToSymbolMap = require('currency-symbol-map/map')

// ES6
import currencyToSymbolMap from 'currency-symbol-map/map'

console.log(currencyToSymbolMap)
// =>
{
 "USD": "$",
 "GBP": "£",}

Tests

npm test

Changelog

5.1.0

  • Add CNH Currency mapping: #82

5.0.1

  • upgraded linter and improved code formatting (#63)

5.0.0

  • added some missing currencies (#61)
  • fixed the BTC symbol (#50)
  • fixed the KZT symbol (#52)
  • fixed the SGD symbol (#55)
  • added TypeScript typings (#53)
  • upgraded test libraries & added extra test cases (#62)

4.0.0

  • the reverse lookup feature was removed (retrieving currency given a symbol) because there is not a deterministic way to do so (i.e. the same symbol is used by multiple currencies).
  • in previous versions, an unsuccessful lookup would return the '?' character. It now returns undefined so that it is up to you how to handle the failure.

Credits

Currency symbols originally sourced from xe, but maintained and updated by contributors.

currency-symbol-map's People

Contributors

auo avatar avoyage avatar bengourley avatar bestan avatar carmelc avatar davericher avatar david90 avatar garylee1 avatar hmharshit avatar k-funk avatar kunshao-msft avatar lafama avatar lihop avatar linusu avatar mattgallivan avatar matthewboman avatar michaelrhodes avatar namenyi avatar ninagittle avatar phoenixbox avatar sarxos avatar thoth-ibl avatar undeadlol1 avatar yiin avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar

currency-symbol-map's Issues

Currency name

I think it would be good to add a function to get the currency name as well as the symbol.

Maybe add a getNameFromCurrency function that would return the name based on the currency code, and another one (e.g. getCurrency) that would get both the symbol and the name (e.g. {symbol: '$', name: 'US Dollar'}).

I would be willing to create a PR if you like the idea.

CHF

Great map, guys! Just FYI, the one symbol you are missing, the Swiss franc (CHF) is commonly abbreviated Fr in Switzerland.

Not a 1-1 mapping between currencies and symbols

While I was reviewing the tests for #22, I noticed an issue. @bestan this relates to the work you did to implement the reverse mapping from symbols back to currencies (#13).

I don't if it was the situation at the time (I haven't checked) but it certainly is the case now – there's a 1-many mapping between symbols and currency codes. This means working out the code from the symbol is non-deterministic. Because of the way it's implemented, the actual implementation chooses the last code to have been added to the map.

So for example before the changes in #22:

getCurrencyFromSymbol('$') === 'USD'

and after:

getCurrencyFromSymbol('$') === 'XCD'

Any thoughts @bestan?

Not usable via CDN

I have a Shopify site where I need to dynamically convert from a currency code to a symbol. However being in Shopify I can't use NPM modules.

I'm happy to do a PR for a webpack build so that this library can be used via a CDN.

Symbol left or right?

Would be great if you could get a symbol object with say {left: '', right: '€'} or {left: '£', right: ''}. So you could write:
getSymbolFromCurrency('GBP'); //=> {left: '£', right: ''}

License

Would you be willing to attach a license to this code (preferably MIT)?

Thanks!

Throwing a TypeError when input isn't string

How about throwing a TypeError when input isn't a string? Currently getSymbolFromCurrency(1) returns undefined, but it seems very likely that it would only be due to programmer error that something non-string was submitted.

I'd be happy to submit a PR if you want it 🙌

Some symbols are different with XE

Hi team,

This repo is really helpful for me. Thank you guys. Found a small issue, seems some symbols are different with XE, please see following list,

Code Symbol here Symbol in XE
AZN ман
BOB Bs. $b
CHF Fr. CHF
KZT лв

ES6 Documentation

I'm currently using this in a project written in ES6 / ECMAScript2015. If you'd like, I can add an ES6 usage example to the docs.

ES6 modules import within a native ES modules library, Rollup setup

hi guys!

Has anybody been able to successfully consume this library in another, native ES modules-code library? I quickly browsed through dependents on npm but none seem to be natively in ES modules.

I'm rewriting one of my libs that consumes this library to be in ES modules and I'm having trouble with Rollup. It does not recognise currency-symbol-map/map (imported by src/util.js) (as per readme instructions):

screen shot 2017-11-12 at 21 10 10

I am following the readme instructions though, import currencyToSymbolMap from 'currency-symbol-map/map'

Looking at the setup of this library, I find it strange. Correct me if I'm wrong, but in order to properly support ES modules library should be rewritten in ES modules and end-file path to be wired up to module key in package.json. For that you need Rollup and Babel plugin and transpile a copy to CommonJS and wire up as main in package.json.

For a proper ES modules setup it's not enough just to wire it up via module.exports in main JS file, is it?


For the record I'm not a Rollup n00b, I have published whole chains of open source native ES module libraries consuming other libraries and I'm following Rollup template. If I can't get it working others probably can't either (or they probably haven't jumped onto ES modules bandwagon yet).


On the positive side, that's an awesome library.

cheers
Roy

Cents and pence?

Is it possible to get the smaller unit symbol too? Like USD has cents and GBP has pence?

Type not found for currency-symbol-map/map

Hi,

When importing all currency symbols:

import currencyToSymbolMap from 'currency-symbol-map/map'

I have a type error, module file declaration 'currency-symbol-map/map' not found.

I added this declaration in my project:

type CurrencySymbolMapMap = {
  [index: string]: string;
};
declare module 'currency-symbol-map/map' {
  import data from 'currency-symbol-map/map';

  export default data as CurrencySymbolMapMap;
}

It seems to work, but is it possible to include an "official" declaration directly in the module?

Thank you for your fantastic module :)

Currency symbols displays as empty squares in chrome (windows only)

I'm using this package at client-side as browser script via browserify.
It works fine for Linux and Mac (tested in chrome, safari and firefox), but Windows machines cannot recognize currency symbols, and it looks like this:

https://prnt.sc/hau1xj

I don't know how and why this happens, but if you have the same problem, you can try to use HTML entities instead of utf8 symbols.

Check out this gist - it contains currency codes mapped to the html entities: https://gist.github.com/Gibbs/3920259#gistcomment-2259740

IQD Symbol is incorrect

IQD, the symbol for the Iraqi Dinar Symbol is incorrect.

currently listed in the package: ع.د
actual symbol: د.ع

Canadian sign - CAD

Is there any support for Canadian dollar sign?
We should separate it then USD

Getting undefined when using ES6 import statement in Nestjs.

First of all thank you for the amazing library.

Here is the issue I am getting, the value of getSymbolFromCurrency is undefined when I am importing using ES6 import as follows:

import getSymbolFromCurrency from 'currency-symbol-map';

version:
"currency-symbol-map": "^5.1.0"

Currently I am using the commonjs require method as a workaround.
Please let me know how to fix it.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.