index.js 2.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586
  1. var typecast = require('../');
  2. var assert = require('assert');
  3. describe('.string()', function () {
  4. it('should return a string', function () {
  5. assert(typecast.string(2) === '2');
  6. assert(typeof typecast.string({}) == 'string');
  7. });
  8. it('should return an empty string when given a null-ish value', function () {
  9. assert(typecast.string(null) === '');
  10. assert(typecast.string(undefined) === '');
  11. });
  12. });
  13. describe('.number()', function () {
  14. it('should return a number', function () {
  15. assert(typecast.number('123') === 123);
  16. });
  17. it('should return 0 if typecasting fails', function () {
  18. assert(typecast.number('abc') === 0);
  19. });
  20. it('should return an 0 when given a null-ish value', function () {
  21. assert(typecast.number(null) === 0);
  22. assert(typecast.number(undefined) === 0);
  23. });
  24. });
  25. describe('.date()', function () {
  26. it('should return a date', function () {
  27. assert(typecast.date('2010 10 01').valueOf() === 1285884000000);
  28. });
  29. it('should return default date if typecasting fails', function () {
  30. assert(typecast.date('abc') instanceof Date);
  31. assert(typecast.date('abc').valueOf() == 0);
  32. });
  33. it('should return default date when given a null-ish value', function () {
  34. assert(typecast.date(null) instanceof Date);
  35. assert(typecast.date(null).valueOf() == 0);
  36. assert(typecast.date(undefined) instanceof Date);
  37. assert(typecast.date(undefined).valueOf() == 0);
  38. });
  39. });
  40. describe('.array()', function () {
  41. it('should return an array', function () {
  42. var arr = [1, 2, 3];
  43. assert(typecast.array(arr) === arr);
  44. assert(typecast.array(1) instanceof Array);
  45. assert(typecast.array('a, b, c').length == 3);
  46. assert(typecast.array('a, b, c')[1] === 'b');
  47. });
  48. it('should preserve non-string objects', function () {
  49. var now = new Date();
  50. assert(Array.isArray(typecast.array(now)));
  51. assert(typecast.array(now).length == 1);
  52. assert(typecast.array(now)[0] === now);
  53. });
  54. it('should return an empty array when given a null-ish value', function () {
  55. assert(Array.isArray(typecast.array(null)));
  56. assert(typecast.array(null).length == 0);
  57. assert(Array.isArray(typecast.array(undefined)));
  58. assert(typecast.array(undefined).length == 0);
  59. });
  60. });
  61. describe('.boolean()', function () {
  62. it('should return a boolean', function () {
  63. assert(typecast.boolean('abc') === true);
  64. assert(typecast.boolean(0) === false);
  65. assert(typecast.boolean('0') === false);
  66. assert(typecast.boolean('false') === false);
  67. });
  68. });
  69. describe('typecast()', function () {
  70. it('should work', function () {
  71. assert(typecast(123, 'string') === '123');
  72. });
  73. it('should throw when given an invalid type', function () {
  74. var err;
  75. try {
  76. typecast(1, 'invalid');
  77. } catch (e) {
  78. err = e;
  79. }
  80. assert(err);
  81. });
  82. });