index.js 621 B

123456789101112131415161718192021222324252627282930313233343536
  1. 'use strict';
  2. const invertKv = require('invert-kv');
  3. const all = require('./lcid.json');
  4. const inverted = invertKv(all);
  5. exports.from = lcidCode => {
  6. if (typeof lcidCode !== 'number') {
  7. throw new TypeError('Expected a number');
  8. }
  9. return all[lcidCode];
  10. };
  11. exports.to = localeId => {
  12. if (typeof localeId !== 'string') {
  13. throw new TypeError('Expected a string');
  14. }
  15. const lcidCode = inverted[localeId];
  16. if (lcidCode) {
  17. return Number(inverted[localeId]);
  18. }
  19. };
  20. exports.all = new Proxy(
  21. inverted,
  22. {
  23. get(target, name) {
  24. const lcid = target[name];
  25. if (lcid) {
  26. return Number(lcid);
  27. }
  28. }
  29. }
  30. );