@astronautlabs/jsonpath
Version:
Query JavaScript objects with JSONPath expressions. Robust / safe JSONPath engine for Node.js.
45 lines • 1.91 kB
JavaScript
import * as assert from 'assert';
import { suite } from 'razmin';
import { slice } from './slice';
var data = ['a', 'b', 'c', 'd', 'e', 'f'];
suite(function (describe) {
describe('slice', function (it) {
it('no params yields copy', function () {
assert.deepEqual(slice(data), data);
});
it('no end param defaults to end', function () {
assert.deepEqual(slice(data, 2), data.slice(2));
});
it('zero end param yields empty', function () {
assert.deepEqual(slice(data, 0, 0), []);
});
it('first element with explicit params', function () {
assert.deepEqual(slice(data, 0, 1, 1), ['a']);
});
it('last element with explicit params', function () {
assert.deepEqual(slice(data, -1, 6), ['f']);
});
it('empty extents and negative step reverses', function () {
assert.deepEqual(slice(data, null, null, -1), ['f', 'e', 'd', 'c', 'b', 'a']);
});
it('negative step partial slice', function () {
assert.deepEqual(slice(data, 4, 2, -1), ['e', 'd']);
});
it('negative step partial slice no start defaults to end', function () {
assert.deepEqual(slice(data, null, 2, -1), ['f', 'e', 'd']);
});
it('extents clamped end', function () {
assert.deepEqual(slice(data, null, 100), data);
});
it('extents clamped beginning', function () {
assert.deepEqual(slice(data, -100, 100), data);
});
it('backwards extents yields empty', function () {
assert.deepEqual(slice(data, 2, 1), []);
});
it('zero step gets shot down', function () {
assert.throws(function () { slice(data, null, null, 0); });
});
});
});
//# sourceMappingURL=slice.test.js.map