src/utils/index.js

  1. 'use strict';
  2. function isNaN( ...n ) {
  3. return !n.filter( ( _n ) => ( _n === _n ) ).length;
  4. }
  5. function isUndefined( ...n ) {
  6. return !n.filter( ( _n ) => ( _n !== undefined ) ).length;
  7. }
  8. function isNull( ...n ) {
  9. return !n.filter( ( _n ) => ( _n !== null ) ).length;
  10. }
  11. function isBoolean( ...n ) {
  12. return !n.filter( ( _n ) => ( !!_n !== _n ) ).length;
  13. }
  14. function isString( ...n ) {
  15. return !n.filter( ( _n ) => ( '' + _n !== _n ) ).length;
  16. }
  17. function isNumber( ...n ) {
  18. return !n.filter( ( _n ) => ( +_n !== _n ) ).length;
  19. }
  20. function isPrimitive( ...n ) {
  21. return n.filter( ( _n ) => ( isBoolean( _n ) || isString( _n ) || isNumber( _n ) ) ).length === n.length;
  22. }
  23. function isArray( ...n ) {
  24. return n.filter( ( _n ) => Array.isArray( _n ) ).length === n.length;
  25. }
  26. function isObject( ...n ) {
  27. return n.filter( ( _n ) => !isArray( _n ) && typeof _n === 'object' ).length === n.length;
  28. }
  29. function isBuffer( ...n ) {
  30. return n.filter( ( _n ) => Buffer.isBuffer( _n ) ).length === n.length;
  31. }
  32. function isFunction( ...n ) {
  33. return n.filter( ( _n ) => typeof _n === 'function' ).length === n.length;
  34. }
  35. function isValue( ...n ) {
  36. return n.filter( ( _n ) => !( isNaN( _n ) || isUndefined( _n ) || isNull( _n ) ) ).length === n.length;
  37. }
  38. /**
  39. * wait
  40. * @description
  41. * similar to <code>usleep</code> in C++
  42. * @param {milliseconds} t - time to wait
  43. * @param {*} [v] - optional value to pass through when promise resolves
  44. * @returns {Promise<any>} - promise to be resolved in [t] milliseconds
  45. * @example
  46. * wait( 100, 'hello world' )
  47. * .then() // -> will pass 'hello world' in 100ms
  48. */
  49. function wait( t, v ) {
  50. if ( !isNumber( t ) ) {
  51. throw new Error( 'Argument Error - expected number' );
  52. }
  53. return new Promise(
  54. ( res ) => setTimeout( () => res( v ), t )
  55. );
  56. }
  57. /**
  58. * bytesToSize
  59. * @description
  60. * Convert bytes to human readable format
  61. * @param {bytes} bytes - unit in bytes to parse
  62. * @returns {string} - pretty string in the format [n unit]
  63. * @example
  64. * bytesToSize( 1073741824 ) // -> 1 GB
  65. */
  66. function bytesToSize( bytes ) {
  67. if ( !bytes ) {
  68. return '0 Byte';
  69. }
  70. const
  71. sizes = [ 'Bytes', 'KB', 'MB', 'GB', 'TB' ],
  72. i = ~~( Math.log( bytes ) / Math.log( 1024 ) );
  73. return Math.round( bytes / Math.pow( 1024, i ) ) + ' ' + sizes[ i ];
  74. }
  75. function isPowerOfTwo( n ) {
  76. return n && !( n & ( n - 1 ) );
  77. }
  78. module.exports = {
  79. isNaN,
  80. isUndefined,
  81. isNull,
  82. isBoolean,
  83. isString,
  84. isNumber,
  85. isPrimitive,
  86. isArray,
  87. isObject,
  88. isBuffer,
  89. isFunction,
  90. isValue,
  91. wait,
  92. bytesToSize,
  93. isPowerOfTwo
  94. };