stringToParts.js 1.0 KB

123456789101112131415161718192021222324252627282930
  1. 'use strict';
  2. const assert = require('assert');
  3. const stringToParts = require('../lib/stringToParts');
  4. describe('stringToParts', function() {
  5. it('handles brackets for numbers', function() {
  6. assert.deepEqual(stringToParts('list[0].name'), ['list', '0', 'name']);
  7. assert.deepEqual(stringToParts('list[0][1].name'), ['list', '0', '1', 'name']);
  8. });
  9. it('handles dot notation', function() {
  10. assert.deepEqual(stringToParts('a.b.c'), ['a', 'b', 'c']);
  11. assert.deepEqual(stringToParts('a..b.d'), ['a', '', 'b', 'd']);
  12. });
  13. it('ignores invalid numbers in square brackets', function() {
  14. assert.deepEqual(stringToParts('foo[1mystring]'), ['foo[1mystring]']);
  15. assert.deepEqual(stringToParts('foo[1mystring].bar[1]'), ['foo[1mystring]', 'bar', '1']);
  16. assert.deepEqual(stringToParts('foo[1mystring][2]'), ['foo[1mystring]', '2']);
  17. });
  18. it('handles empty string', function() {
  19. assert.deepEqual(stringToParts(''), ['']);
  20. });
  21. it('handles trailing dot', function() {
  22. assert.deepEqual(stringToParts('a.b.'), ['a', 'b', '']);
  23. });
  24. });