metadata.test.js 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180
  1. function _typeof(obj) { "@babel/helpers - typeof"; return _typeof = "function" == typeof Symbol && "symbol" == typeof Symbol.iterator ? function (obj) { return typeof obj; } : function (obj) { return obj && "function" == typeof Symbol && obj.constructor === Symbol && obj !== Symbol.prototype ? "symbol" : typeof obj; }, _typeof(obj); }
  2. import metadata from '../metadata.min.json' assert { type: 'json' };
  3. import metadataV1 from '../test/metadata/1.0.0/metadata.min.json' assert { type: 'json' };
  4. import metadataV2 from '../test/metadata/1.1.11/metadata.min.json' assert { type: 'json' };
  5. import metadataV3 from '../test/metadata/1.7.34/metadata.min.json' assert { type: 'json' };
  6. import metadataV4 from '../test/metadata/1.7.37/metadata.min.json' assert { type: 'json' };
  7. import Metadata, { validateMetadata, getExtPrefix, isSupportedCountry } from './metadata.js';
  8. describe('metadata', function () {
  9. it('should return undefined for non-defined types', function () {
  10. var FR = new Metadata(metadata).country('FR');
  11. type(FR.type('FIXED_LINE')).should.equal('undefined');
  12. });
  13. it('should validate country', function () {
  14. var thrower = function thrower() {
  15. return new Metadata(metadata).country('RUS');
  16. };
  17. thrower.should["throw"]('Unknown country');
  18. });
  19. it('should tell if a country is supported', function () {
  20. isSupportedCountry('RU', metadata).should.equal(true);
  21. isSupportedCountry('XX', metadata).should.equal(false);
  22. });
  23. it('should return ext prefix for a country', function () {
  24. getExtPrefix('US', metadata).should.equal(' ext. ');
  25. getExtPrefix('CA', metadata).should.equal(' ext. ');
  26. getExtPrefix('GB', metadata).should.equal(' x'); // expect(getExtPrefix('XX', metadata)).to.equal(undefined)
  27. getExtPrefix('XX', metadata).should.equal(' ext. ');
  28. });
  29. it('should cover non-occuring edge cases', function () {
  30. new Metadata(metadata).getNumberingPlanMetadata('999');
  31. });
  32. it('should support deprecated methods', function () {
  33. new Metadata(metadata).country('US').nationalPrefixForParsing().should.equal('1');
  34. new Metadata(metadata).chooseCountryByCountryCallingCode('1').nationalPrefixForParsing().should.equal('1');
  35. });
  36. it('should tell if a national prefix is mandatory when formatting a national number', function () {
  37. var meta = new Metadata(metadata); // No "national_prefix_formatting_rule".
  38. // "national_prefix_is_optional_when_formatting": true
  39. meta.country('US');
  40. meta.numberingPlan.formats()[0].nationalPrefixIsMandatoryWhenFormattingInNationalFormat().should.equal(false); // "national_prefix_formatting_rule": "8 ($1)"
  41. // "national_prefix_is_optional_when_formatting": true
  42. meta.country('RU');
  43. meta.numberingPlan.formats()[0].nationalPrefixIsMandatoryWhenFormattingInNationalFormat().should.equal(false); // "national_prefix": "0"
  44. // "national_prefix_formatting_rule": "0 $1"
  45. meta.country('FR');
  46. meta.numberingPlan.formats()[0].nationalPrefixIsMandatoryWhenFormattingInNationalFormat().should.equal(true);
  47. });
  48. it('should validate metadata', function () {
  49. var thrower = function thrower() {
  50. return validateMetadata();
  51. };
  52. thrower.should["throw"]('`metadata` argument not passed');
  53. thrower = function thrower() {
  54. return validateMetadata(123);
  55. };
  56. thrower.should["throw"]('Got a number: 123.');
  57. thrower = function thrower() {
  58. return validateMetadata('abc');
  59. };
  60. thrower.should["throw"]('Got a string: abc.');
  61. thrower = function thrower() {
  62. return validateMetadata({
  63. a: true,
  64. b: 2
  65. });
  66. };
  67. thrower.should["throw"]('Got an object of shape: { a, b }.');
  68. thrower = function thrower() {
  69. return validateMetadata({
  70. a: true,
  71. countries: 2
  72. });
  73. };
  74. thrower.should["throw"]('Got an object of shape: { a, countries }.');
  75. thrower = function thrower() {
  76. return validateMetadata({
  77. country_calling_codes: true,
  78. countries: 2
  79. });
  80. };
  81. thrower.should["throw"]('Got an object of shape');
  82. thrower = function thrower() {
  83. return validateMetadata({
  84. country_calling_codes: {},
  85. countries: 2
  86. });
  87. };
  88. thrower.should["throw"]('Got an object of shape');
  89. validateMetadata({
  90. country_calling_codes: {},
  91. countries: {},
  92. b: 3
  93. });
  94. });
  95. it('should work around `nonGeographical` typo in metadata generated from `1.7.35` to `1.7.37`', function () {
  96. var meta = new Metadata(metadataV4);
  97. meta.selectNumberingPlan('888');
  98. type(meta.nonGeographic()).should.equal('object');
  99. });
  100. it('should work around `nonGeographic` metadata not existing before `1.7.35`', function () {
  101. var meta = new Metadata(metadataV3);
  102. type(meta.getNumberingPlanMetadata('800')).should.equal('object');
  103. type(meta.getNumberingPlanMetadata('000')).should.equal('undefined');
  104. });
  105. it('should work with metadata from version `1.1.11`', function () {
  106. var meta = new Metadata(metadataV2);
  107. meta.selectNumberingPlan('US');
  108. meta.numberingPlan.possibleLengths().should.deep.equal([10]);
  109. meta.numberingPlan.formats().length.should.equal(1);
  110. meta.numberingPlan.nationalPrefix().should.equal('1');
  111. meta.numberingPlan.nationalPrefixForParsing().should.equal('1');
  112. meta.numberingPlan.type('MOBILE').pattern().should.equal('');
  113. meta.selectNumberingPlan('AG');
  114. meta.numberingPlan.leadingDigits().should.equal('268'); // Should've been "268$1" but apparently there was a bug in metadata generator
  115. // and no national prefix transform rules were written.
  116. expect(meta.numberingPlan.nationalPrefixTransformRule()).to.be["null"];
  117. meta.selectNumberingPlan('AF');
  118. meta.numberingPlan.formats()[0].nationalPrefixFormattingRule().should.equal('0$1');
  119. meta.selectNumberingPlan('RU');
  120. meta.numberingPlan.formats()[0].nationalPrefixIsOptionalWhenFormattingInNationalFormat().should.equal(true);
  121. });
  122. it('should work with metadata from version `1.0.0`', function () {
  123. var meta = new Metadata(metadataV1);
  124. meta.selectNumberingPlan('US');
  125. meta.numberingPlan.formats().length.should.equal(1);
  126. meta.numberingPlan.nationalPrefix().should.equal('1');
  127. meta.numberingPlan.nationalPrefixForParsing().should.equal('1');
  128. type(meta.numberingPlan.type('MOBILE')).should.equal('undefined');
  129. meta.selectNumberingPlan('AG');
  130. meta.numberingPlan.leadingDigits().should.equal('268'); // Should've been "268$1" but apparently there was a bug in metadata generator
  131. // and no national prefix transform rules were written.
  132. expect(meta.numberingPlan.nationalPrefixTransformRule()).to.be["null"];
  133. meta.selectNumberingPlan('AF');
  134. meta.numberingPlan.formats()[0].nationalPrefixFormattingRule().should.equal('0$1');
  135. meta.selectNumberingPlan('RU');
  136. meta.numberingPlan.formats()[0].nationalPrefixIsOptionalWhenFormattingInNationalFormat().should.equal(true);
  137. });
  138. it('should work around "ext" data not present in metadata from version `1.0.0`', function () {
  139. var meta = new Metadata(metadataV1);
  140. meta.selectNumberingPlan('GB');
  141. meta.ext().should.equal(' ext. ');
  142. var metaNew = new Metadata(metadata);
  143. metaNew.selectNumberingPlan('GB');
  144. metaNew.ext().should.equal(' x');
  145. });
  146. it('should work around "default IDD prefix" data not present in metadata from version `1.0.0`', function () {
  147. var meta = new Metadata(metadataV1);
  148. meta.selectNumberingPlan('AU');
  149. type(meta.defaultIDDPrefix()).should.equal('undefined');
  150. var metaNew = new Metadata(metadata);
  151. metaNew.selectNumberingPlan('AU');
  152. metaNew.defaultIDDPrefix().should.equal('0011');
  153. });
  154. });
  155. function type(something) {
  156. return _typeof(something);
  157. }
  158. //# sourceMappingURL=metadata.test.js.map