@blinkk/editor
Version:
Structured content editor with live previews.
231 lines • 6.51 kB
JavaScript
"use strict";
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
const deepWalk_1 = require("./deepWalk");
const dataType_1 = require("@blinkk/selective-edit/dist/src/utility/dataType");
const ava_1 = __importDefault(require("ava"));
ava_1.default('selectively replace strings in record', async (t) => {
t.plan(1);
const walker = new deepWalk_1.DeepWalk({});
const transformValue = async (value) => {
if (dataType_1.DataType.isString(value)) {
return `foo${value}`;
}
return value;
};
t.deepEqual(await walker.walk({
foo: ['bar', ['bub'], { eeb: 'loo' }],
test: {
floo: 'baz',
bab: ['tar'],
subtest: {
hub: 'web',
},
},
bar: 1,
baz: 'boo',
}, transformValue), {
foo: ['foobar', ['foobub'], { eeb: 'fooloo' }],
test: {
floo: 'foobaz',
bab: ['footar'],
subtest: {
hub: 'fooweb',
},
},
bar: 1,
baz: 'fooboo',
});
});
ava_1.default('selectively replace strings in array', async (t) => {
t.plan(1);
const walker = new deepWalk_1.DeepWalk({});
const transformValue = async (value) => {
if (dataType_1.DataType.isString(value)) {
return `foo${value}`;
}
return value;
};
t.deepEqual(await walker.walk(['bar', ['bub'], { eeb: 'loo' }], transformValue), ['foobar', ['foobub'], { eeb: 'fooloo' }]);
});
ava_1.default('selectively replace array custom', async (t) => {
t.plan(1);
class TestCustomWalker extends deepWalk_1.DeepWalk {
async walkArray(
// eslint-disable-next-line @typescript-eslint/no-unused-vars
originalValue,
// eslint-disable-next-line @typescript-eslint/no-unused-vars
transformValue) {
return ['foo'];
}
}
const walker = new TestCustomWalker({});
const transformValue = async (value) => {
if (dataType_1.DataType.isString(value)) {
return `foo${value}`;
}
return value;
};
t.deepEqual(await walker.walk({
foo: ['bar'],
test: {
floo: 'baz',
},
bar: 1,
baz: 'boo',
}, transformValue), {
foo: ['foo'],
test: {
floo: 'foobaz',
},
bar: 1,
baz: 'fooboo',
});
});
ava_1.default('selectively replace objects custom', async (t) => {
t.plan(1);
class TestCustomWalker extends deepWalk_1.DeepWalk {
async walkRecord(
// eslint-disable-next-line @typescript-eslint/no-unused-vars
originalValue,
// eslint-disable-next-line @typescript-eslint/no-unused-vars
transformValue) {
return {
foo: true,
};
}
}
const walker = new TestCustomWalker({});
const transformValue = async (value) => {
if (dataType_1.DataType.isString(value)) {
return `foo${value}`;
}
return value;
};
t.deepEqual(await walker.walk({
foo: ['bar'],
test: {
floo: 'baz',
},
bar: 1,
baz: 'boo',
}, transformValue), {
foo: true,
});
});
ava_1.default('selectively replace strings in record sync', t => {
t.plan(1);
const walker = new deepWalk_1.DeepWalk({});
const transformValue = (value) => {
if (dataType_1.DataType.isString(value)) {
return `foo${value}`;
}
return value;
};
t.deepEqual(walker.walkSync({
foo: ['bar', ['bub'], { eeb: 'loo' }],
test: {
floo: 'baz',
bab: ['tar'],
subtest: {
hub: 'web',
},
},
bar: 1,
baz: 'boo',
}, transformValue), {
foo: ['foobar', ['foobub'], { eeb: 'fooloo' }],
test: {
floo: 'foobaz',
bab: ['footar'],
subtest: {
hub: 'fooweb',
},
},
bar: 1,
baz: 'fooboo',
});
});
ava_1.default('selectively replace strings in array sync', t => {
t.plan(1);
const walker = new deepWalk_1.DeepWalk({});
const transformValue = (value) => {
if (dataType_1.DataType.isString(value)) {
return `foo${value}`;
}
return value;
};
t.deepEqual(walker.walkSync(['bar', ['bub'], { eeb: 'loo' }], transformValue), [
'foobar',
['foobub'],
{ eeb: 'fooloo' },
]);
});
ava_1.default('selectively replace array custom sync', t => {
t.plan(1);
class TestCustomWalker extends deepWalk_1.DeepWalk {
walkArraySync(
// eslint-disable-next-line @typescript-eslint/no-unused-vars
originalValue,
// eslint-disable-next-line @typescript-eslint/no-unused-vars
transformValue) {
return ['foo'];
}
}
const walker = new TestCustomWalker({});
const transformValue = (value) => {
if (dataType_1.DataType.isString(value)) {
return `foo${value}`;
}
return value;
};
t.deepEqual(walker.walkSync({
foo: ['bar'],
test: {
floo: 'baz',
},
bar: 1,
baz: 'boo',
}, transformValue), {
foo: ['foo'],
test: {
floo: 'foobaz',
},
bar: 1,
baz: 'fooboo',
});
});
ava_1.default('selectively replace objects custom sync', t => {
t.plan(1);
class TestCustomWalker extends deepWalk_1.DeepWalk {
walkRecordSync(
// eslint-disable-next-line @typescript-eslint/no-unused-vars
originalValue,
// eslint-disable-next-line @typescript-eslint/no-unused-vars
transformValue) {
return {
foo: true,
};
}
}
const walker = new TestCustomWalker({});
const transformValue = async (value) => {
if (dataType_1.DataType.isString(value)) {
return `foo${value}`;
}
return value;
};
t.deepEqual(walker.walkSync({
foo: ['bar'],
test: {
floo: 'baz',
},
bar: 1,
baz: 'boo',
}, transformValue), {
foo: true,
});
});
//# sourceMappingURL=deepWalk.test.js.map