Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 1 addition & 22 deletions spec/utils.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@
// SOFTWARE.

import { expect } from 'chai';
import { applyChange, normalizePath, pathParts, valAt } from '../src/utils';
import { applyChange, normalizePath, pathParts } from '../src/utils';

describe('utils', () => {
describe('.normalizePath(path: string)', () => {
Expand All @@ -42,27 +42,6 @@ describe('utils', () => {
});
});

describe('.valAt(source: any, path?: string): any', () => {
it('should be null if null along any point in the path', () => {
expect(valAt(null)).to.be.null;
expect(valAt(null, '/foo')).to.be.null;
expect(valAt({ a: { b: null } }, '/a/b/c')).to.be.null;
});

it('should be null if accessing a path past a leaf value', () => {
expect(valAt({ a: 2 }, '/a/b')).to.be.null;
});

it('should be the leaf value if one is present', () => {
expect(valAt({ a: { b: 23 } }, '/a/b')).to.eq(23);
expect(valAt({ a: { b: 23 } }, '/a')).to.deep.equal({ b: 23 });
});

it('should be undefined if in unexplored territory', () => {
expect(valAt({ a: 23 }, '/b')).to.be.undefined;
});
});

describe('.applyChange(from: any, to: any): any', () => {
it('should return the to value for non-object values of from and to', () => {
expect(applyChange({ a: 'b' }, null)).to.eq(null);
Expand Down
31 changes: 0 additions & 31 deletions src/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -61,34 +61,3 @@ export function pruneNulls(obj: any) {
}
return obj;
}

export function valAt(source: any, path?: string) {
if (source === null) {
return null;
} else if (typeof source !== 'object') {
return path ? null : source;
}

const parts = pathParts(path);
if (!parts.length) {
return source;
}

let cur = source;
let leaf;
while (parts.length) {
const key = parts.shift();
if (cur[key] === null || leaf) {
return null;
} else if (typeof cur[key] === 'object') {
if (parts.length) {
cur = cur[key];
} else {
return cur[key];
}
} else {
leaf = cur[key];
}
}
return leaf;
}