slice.js 674 B

12345678910111213141516171819202122232425262728
  1. var utils = require('../utils'),
  2. nodes = require('../nodes');
  3. /**
  4. * This is a helper function for the slice method
  5. *
  6. * @param {String|Ident} vals
  7. * @param {Unit} start [0]
  8. * @param {Unit} end [vals.length]
  9. * @return {String|Literal|Null}
  10. * @api public
  11. */
  12. (module.exports = function slice(val, start, end) {
  13. start = start && start.nodes[0].val;
  14. end = end && end.nodes[0].val;
  15. val = utils.unwrap(val).nodes;
  16. if (val.length > 1) {
  17. return utils.coerce(val.slice(start, end), true);
  18. }
  19. var result = val[0].string.slice(start, end);
  20. return val[0] instanceof nodes.Ident
  21. ? new nodes.Ident(result)
  22. : new nodes.String(result);
  23. }).raw = true;