numeral.js 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475
  1. // Node
  2. if (typeof module !== 'undefined' && module.exports) {
  3. var numeral = require('../numeral');
  4. var expect = require('chai').expect;
  5. }
  6. describe('Numeral', function() {
  7. afterEach(function() {
  8. numeral.reset();
  9. });
  10. describe('Default', function() {
  11. it('should set a default format', function() {
  12. numeral.defaultFormat('0,0');
  13. expect(numeral(10000).format()).to.equal('10,000');
  14. });
  15. });
  16. describe('Types', function() {
  17. it('should return a value as correct type', function() {
  18. var tests = [
  19. [1234.56,'number'],
  20. ['1234.56','number'],
  21. [0,'number'],
  22. [NaN,'object'],
  23. [null,'object']
  24. ],
  25. i;
  26. for (i = 0; i < tests.length; i++) {
  27. expect(typeof numeral(tests[i][0]).value()).to.equal(tests[i][1]);
  28. }
  29. });
  30. });
  31. describe('Value', function() {
  32. it('should return a value', function() {
  33. var tests = [
  34. [1000, 1000],
  35. [0.5, 0.5],
  36. [null, null],
  37. ['1,000', 1000],
  38. ['not a number', null]
  39. ],
  40. num;
  41. for (var i = 0; i < tests.length; i++) {
  42. num = numeral(tests[i][0]);
  43. expect(num.value()).to.equal(tests[i][1]);
  44. }
  45. });
  46. });
  47. describe('Set', function() {
  48. it('should set a value', function() {
  49. var tests = [
  50. [1000,1000],
  51. [-0.25,-0.25]
  52. ],
  53. num;
  54. for (var i = 0; i < tests.length; i++) {
  55. num = numeral().set(tests[i][0]);
  56. expect(num.value()).to.equal(tests[i][1]);
  57. }
  58. });
  59. });
  60. describe('Custom Zero', function() {
  61. it('should change zero value', function() {
  62. var tests = [
  63. [0,null,'0','0'],
  64. [0,'N/A','0','N/A'],
  65. [0,'','','']
  66. ];
  67. for (var i = 0; i < tests.length; i++) {
  68. numeral.zeroFormat(tests[i][1]);
  69. expect(numeral(tests[i][0]).format(tests[i][2])).to.equal(tests[i][3]);
  70. }
  71. });
  72. });
  73. describe('Custom Null', function() {
  74. it('should change null value', function() {
  75. var tests = [
  76. [null,null,'0','0'],
  77. [null,'N/A','0','N/A'],
  78. [null,'','','']
  79. ];
  80. for (var i = 0; i < tests.length; i++) {
  81. numeral.nullFormat(tests[i][1]);
  82. expect(numeral(tests[i][0]).format(tests[i][2])).to.equal(tests[i][3]);
  83. }
  84. });
  85. });
  86. describe('Clone', function() {
  87. it('should clone', function() {
  88. var a = numeral(1000),
  89. b = numeral(a),
  90. c = a.clone(),
  91. aVal = a.value(),
  92. aSet = a.set(2000).value(),
  93. bVal = b.value(),
  94. cVal = c.add(10).value();
  95. expect(aVal).to.equal(1000);
  96. expect(aSet).to.equal(2000);
  97. expect(bVal).to.equal(1000);
  98. expect(cVal).to.equal(1010);
  99. });
  100. });
  101. describe('isNumeral', function() {
  102. it('should return boolean', function() {
  103. var tests = [
  104. [numeral(),true],
  105. [1,false]
  106. ];
  107. for (var i = 0; i < tests.length; i++) {
  108. expect(numeral.isNumeral(tests[i][0])).to.equal(tests[i][1]);
  109. }
  110. });
  111. });
  112. describe('Format', function() {
  113. it('should format to a number', function() {
  114. var tests = [
  115. [0, null, '0'],
  116. [0, '0.00', '0.00'],
  117. [null, null, '0'],
  118. [NaN, '0.0', '0.0'],
  119. [1.23,'0,0','1'],
  120. [10000,'0,0.0000','10,000.0000'],
  121. [10000.23,'0,0','10,000'],
  122. [-10000,'0,0.0','-10,000.0'],
  123. [10000.1234,'0.000','10000.123'],
  124. [10000,'0[.]00','10000'],
  125. [10000.1,'0[.]00','10000.10'],
  126. [10000.123,'0[.]00','10000.12'],
  127. [10000.456,'0[.]00','10000.46'],
  128. [10000.001,'0[.]00','10000'],
  129. [10000.45,'0[.]00[0]','10000.45'],
  130. [10000.456,'0[.]00[0]','10000.456'],
  131. [10000,'(0,0.0000)','10,000.0000'],
  132. [-10000,'(0,0.0000)','(10,000.0000)'],
  133. [-12300,'+0,0.0000','-12,300.0000'],
  134. [1230,'+0,0','+1,230'],
  135. [1230,'-0,0','1,230'],
  136. [-1230,'-0,0','-1,230'],
  137. [-1230.4,'0,0.0+','1,230.4-'],
  138. [-1230.4,'0,0.0-','1,230.4-'],
  139. [1230.4,'0,0.0-','1,230.4'],
  140. [100.78, '0', '101'],
  141. [100.28, '0', '100'],
  142. [1.932,'0.0','1.9'],
  143. [1.9687,'0','2'],
  144. [1.9687,'0.0','2.0'],
  145. [-0.23,'.00','-.23'],
  146. [-0.23,'(.00)','(.23)'],
  147. [0.23,'0.00000','0.23000'],
  148. [0.67,'0.0[0000]','0.67'],
  149. [3162.63,'0.0[00000000000000]','3162.63'],
  150. [1.99,'0.[0]','2'],
  151. [1.0501,'0.00[0]','1.05'],
  152. [1.005,'0.00','1.01'],
  153. // leading zero
  154. [0, '00.0', '00.0'],
  155. [0.23, '000.[00]', '000.23'],
  156. [4, '000', '004'],
  157. [10, '00000', '00010'],
  158. [1000, '000,0', '1,000'],
  159. [1000, '00000,0', '01,000'],
  160. [1000, '0000000,0', '0,001,000'],
  161. // abbreviations
  162. [2000000000,'0.0a','2.0b'],
  163. [1230974,'0.0a','1.2m'],
  164. [1460,'0a','1k'],
  165. [-104000,'0 a','-104 k'],
  166. [999950,'0.0a','1.0m'],
  167. [999999999,'0a','1b'],
  168. // forced abbreviations
  169. [-5444333222111, '0,0 ak', '-5,444,333,222 k'],
  170. [5444333222111, '0,0 am', '5,444,333 m'],
  171. [-5444333222111, '0,0 ab', '-5,444 b'],
  172. [-5444333222111, '0,0 at', '-5 t'],
  173. [123456, '0.0[0] ak', '123.46 k'],
  174. [150,'0.0 ak','0.2 k']
  175. ],
  176. i,
  177. n,
  178. output;
  179. for (i = 0; i < tests.length; i++) {
  180. n = numeral(tests[i][0]);
  181. output = n.format(tests[i][1]);
  182. expect(output).to.equal(tests[i][2]);
  183. expect(typeof output).to.equal('string');
  184. }
  185. });
  186. });
  187. describe('Unformat', function() {
  188. before(function() {
  189. numeral.zeroFormat('N/A');
  190. numeral.nullFormat('N/A');
  191. });
  192. after(function() {
  193. numeral.reset();
  194. });
  195. it('should unformat a number', function() {
  196. var tests = [
  197. ['10,000.123', 10000.123],
  198. ['(0.12345)', -0.12345],
  199. ['((--0.12345))', 0.12345],
  200. ['1.23t', 1230000000000],
  201. ['N/A', 0],
  202. ['', null],
  203. // Pass Through for Numbers
  204. [0, 0],
  205. [1, 1],
  206. [1.1, 1.1],
  207. [-0, 0],
  208. [-1, -1],
  209. [-1.1, -1.1]
  210. ];
  211. for (var i = 0; i < tests.length; i++) {
  212. expect(numeral(tests[i][0]).value()).to.equal(tests[i][1]);
  213. }
  214. });
  215. });
  216. describe('Validate', function() {
  217. var locale = 'en';
  218. describe('Numbers', function() {
  219. it('should validate numbers', function() {
  220. var tests = [
  221. ['1000', true],
  222. ['1,000', true],
  223. ['10,0,0', true],
  224. ['10.123', true],
  225. ['1,000.123', true],
  226. ['1000,123.123', true],
  227. ['1000 ', true],
  228. [' 1000 ', true],
  229. [' 1000', true],
  230. [' 1000,100.123', true],
  231. ['1.0,00', false],
  232. ['1.0.00', false],
  233. ['1 000', false],
  234. ['1.000,123', false],
  235. ['1000.', false],
  236. ['1000,', false],
  237. ['10..00', false],
  238. ['10,,00', false],
  239. ['10, 00', false]
  240. ];
  241. for (var i = 0; i < tests.length; i++) {
  242. expect(numeral.validate(tests[i][0], locale)).to.equal(tests[i][1]);
  243. }
  244. });
  245. });
  246. describe('Currency', function() {
  247. it('should validate currency', function() {
  248. var tests = [
  249. ['$1000', true],
  250. ['$1,000', true],
  251. ['$10,0,0', true],
  252. ['$10.123', true],
  253. ['$1,000.123', true],
  254. ['$1000 ', true],
  255. [' $1000 ', true],
  256. [' $1000', true],
  257. [' $1000,100.123', true],
  258. ['$100.123k', true],
  259. ['$100.123m', true],
  260. ['$100.123b', true],
  261. ['$100.123t', true],
  262. ['100,456.123k', true],
  263. [' 100,456.123t ', true],
  264. ['$1,00.123k', true],
  265. ['%100', false],
  266. [' %1.0.00', false],
  267. [' ^1 000 ', false],
  268. ['^1.000 ', false],
  269. ['$ 1000.', false],
  270. ['%1000', false],
  271. ['100,456.123z', false],
  272. ['$100$', false],
  273. ['$100,213.456l', false],
  274. ['aa100,213.456l', false],
  275. ['$100,213.456kk', false]
  276. ];
  277. for (var i = 0; i < tests.length; i++) {
  278. expect(numeral.validate(tests[i][0], locale)).to.equal(tests[i][1]);
  279. }
  280. });
  281. });
  282. });
  283. describe('Manipulate', function() {
  284. describe('Add', function() {
  285. it('should add', function() {
  286. var tests = [
  287. [1000,10,1010],
  288. [0.5,3,3.5],
  289. [-100,200,100],
  290. [0.1,0.2,0.3],
  291. [0.28,0.01,0.29],
  292. [0.289999,0.000001,0.29],
  293. [0.29,0.01,0.3]
  294. ],
  295. num;
  296. for (var i = 0; i < tests.length; i++) {
  297. num = numeral(tests[i][0]);
  298. num.add(tests[i][1]);
  299. expect(num.value()).to.equal(tests[i][2]);
  300. }
  301. });
  302. });
  303. describe('Subtract', function() {
  304. it('should subtract', function() {
  305. var tests = [
  306. [1000,10,990],
  307. [0.5,3,-2.5],
  308. [-100,200,-300],
  309. [0.3,0.1,0.2],
  310. [0.28,0.01,0.27],
  311. [0.29,0.01,0.28]
  312. ],
  313. num;
  314. for (var i = 0; i < tests.length; i++) {
  315. num = numeral(tests[i][0]);
  316. num.subtract(tests[i][1]);
  317. expect(num.value()).to.equal(tests[i][2]);
  318. }
  319. });
  320. });
  321. describe('Add', function() {
  322. it('should add', function() {
  323. });
  324. });
  325. describe('Multiply', function() {
  326. it('should multiply', function() {
  327. var tests = [
  328. [1000,10,10000],
  329. [0.5,3,1.5],
  330. [-100,200,-20000],
  331. [0.1,0.2,0.02],
  332. [0.28,0.01,0.0028],
  333. [0.29,0.01,0.0029],
  334. [0.00000231,10000000,23.1]
  335. ],
  336. num;
  337. for (var i = 0; i < tests.length; i++) {
  338. num = numeral(tests[i][0]);
  339. num.multiply(tests[i][1]);
  340. expect(num.value()).to.equal(tests[i][2]);
  341. }
  342. });
  343. });
  344. describe('Divide', function() {
  345. it('should divide', function() {
  346. var tests = [
  347. [1000,10,100],
  348. [0.5,3,0.16666666666666666],
  349. [-100,200,-0.5],
  350. [5.3,0.1,53],
  351. [0.28,0.01,28],
  352. [0.29,0.01,29]
  353. ],
  354. num;
  355. for (var i = 0; i < tests.length; i++) {
  356. num = numeral(tests[i][0]);
  357. num.divide(tests[i][1]);
  358. expect(num.value()).to.equal(tests[i][2]);
  359. }
  360. });
  361. });
  362. describe('Difference', function() {
  363. it('should find a difference', function() {
  364. var tests = [
  365. [1000,10,990],
  366. [0.5,3,2.5],
  367. [-100,200,300],
  368. [0.3,0.2,0.1],
  369. [0.28,0.01,0.27],
  370. [0.29,0.01,0.28]
  371. ],
  372. num;
  373. for (var i = 0; i < tests.length; i++) {
  374. num = numeral(tests[i][0]);
  375. expect(num.difference(tests[i][1])).to.equal(tests[i][2]);
  376. }
  377. });
  378. });
  379. describe('Rounding', function() {
  380. it('should format with rounding', function() {
  381. var tests = [
  382. // value, format string, expected w/ floor, expected w/ ceil
  383. [2280002, '0.00a', '2.28m', '2.29m'],
  384. [10000.23,'0,0','10,000', '10,001'],
  385. [1000.234,'0,0.00','1,000.23', '1,000.24'],
  386. [0.97487823,'0.000','0.974','0.975'],
  387. [-0.433,'0.0','-0.5', '-0.4']
  388. ],
  389. i;
  390. for (i = 0; i < tests.length; i++) {
  391. // floor
  392. expect(numeral(tests[i][0]).format(tests[i][1], Math.floor)).to.equal(tests[i][2]);
  393. // ceil
  394. expect(numeral(tests[i][0]).format(tests[i][1], Math.ceil)).to.equal(tests[i][3]);
  395. }
  396. });
  397. });
  398. });
  399. describe('Utilities', function() {
  400. describe('Insert', function() {
  401. it('should insert into string', function() {
  402. var tests = [
  403. ['1000', '+', 0, '+1000'],
  404. ['1000', '-', 4, '1000-']
  405. ],
  406. i;
  407. for (i = 0; i < tests.length; i++) {
  408. expect(numeral._.insert(tests[i][0], tests[i][1], tests[i][2])).to.equal(tests[i][3]);
  409. }
  410. });
  411. });
  412. });
  413. });