Skip to content

Commit

Permalink
Minor tweaks (#96)
Browse files Browse the repository at this point in the history
  • Loading branch information
Richienb committed Feb 19, 2022
1 parent aff8acb commit 84c3233
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 9 deletions.
17 changes: 11 additions & 6 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ function getPathSegments(path) {

for (const character of path) {
switch (character) {
case '\\':
case '\\': {
if (currentPart === 'index') {
throw new Error('Invalid character in an index');
}
Expand All @@ -35,8 +35,9 @@ function getPathSegments(path) {
currentPart = 'property';
isIgnoring = !isIgnoring;
break;
}

case '.':
case '.': {
if (currentPart === 'index') {
throw new Error('Invalid character in an index');
}
Expand All @@ -60,8 +61,9 @@ function getPathSegments(path) {
currentSegment = '';
currentPart = 'property';
break;
}

case '[':
case '[': {
if (currentPart === 'index') {
throw new Error('Invalid character in an index');
}
Expand All @@ -88,8 +90,9 @@ function getPathSegments(path) {

currentPart = 'index';
break;
}

case ']':
case ']': {
if (currentPart === 'index') {
parts.push(Number.parseInt(currentSegment, 10));
currentSegment = '';
Expand All @@ -102,8 +105,9 @@ function getPathSegments(path) {
}

// Falls through
}

default:
default: {
if (currentPart === 'index' && !digits.has(character)) {
throw new Error('Invalid character in an index');
}
Expand All @@ -122,6 +126,7 @@ function getPathSegments(path) {
}

currentSegment += character;
}
}
}

Expand Down Expand Up @@ -149,7 +154,7 @@ function getPathSegments(path) {

break;
}
// No default
// No default
}

return parts;
Expand Down
4 changes: 3 additions & 1 deletion index.test-d.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import {expectTypeOf} from 'expect-type';
import {getProperty, setProperty, hasProperty, deleteProperty, deepKeys} from './index.js';
import {getProperty, setProperty, hasProperty, deleteProperty, escapePath, deepKeys} from './index.js';

expectTypeOf(getProperty({foo: {bar: 'unicorn'}}, 'foo.bar')).toBeString();
expectTypeOf(getProperty({foo: {bar: 'a'}}, 'foo.notDefined.deep')).toBeUndefined();
Expand All @@ -18,4 +18,6 @@ expectTypeOf(hasProperty({foo: {bar: 'unicorn'}}, 'foo.bar')).toEqualTypeOf<bool

expectTypeOf(deleteProperty({foo: {bar: 'a'}}, 'foo.bar')).toEqualTypeOf<boolean>();

expectTypeOf(escapePath('foo.bar')).toEqualTypeOf<string>();

expectTypeOf(deepKeys({foo: {bar: 'a'}})).toEqualTypeOf<string[]>();
6 changes: 4 additions & 2 deletions test.js
Original file line number Diff line number Diff line change
Expand Up @@ -411,7 +411,9 @@ test('deepKeys', t => {
const object = {
'a.b': {
c: {
d: [1, 2, 3],
d: [1, 2, {
g: 3,
}],
e: '🦄',
f: 0,
},
Expand All @@ -428,7 +430,7 @@ test('deepKeys', t => {
t.deepEqual(keys, [
'a\\.b.c.d[0]',
'a\\.b.c.d[1]',
'a\\.b.c.d[2]',
'a\\.b.c.d[2].g',
'a\\.b.c.e',
'a\\.b.c.f',
'a\\.b..a',
Expand Down

0 comments on commit 84c3233

Please sign in to comment.