utils.js 888 B

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748
  1. 'use strict';
  2. /**
  3. * Module dependencies
  4. */
  5. exports.extend = require('extend-shallow');
  6. exports.SourceMap = require('source-map');
  7. exports.sourceMapResolve = require('source-map-resolve');
  8. /**
  9. * Convert backslash in the given string to forward slashes
  10. */
  11. exports.unixify = function(fp) {
  12. return fp.split(/\\+/).join('/');
  13. };
  14. /**
  15. * Return true if `val` is a non-empty string
  16. *
  17. * @param {String} `str`
  18. * @return {Boolean}
  19. */
  20. exports.isString = function(str) {
  21. return str && typeof str === 'string';
  22. };
  23. /**
  24. * Cast `val` to an array
  25. * @return {Array}
  26. */
  27. exports.arrayify = function(val) {
  28. if (typeof val === 'string') return [val];
  29. return val ? (Array.isArray(val) ? val : [val]) : [];
  30. };
  31. /**
  32. * Get the last `n` element from the given `array`
  33. * @param {Array} `array`
  34. * @return {*}
  35. */
  36. exports.last = function(arr, n) {
  37. return arr[arr.length - (n || 1)];
  38. };