Skip to content

Commit

Permalink
fix: Refactor unsafe replace function.
Browse files Browse the repository at this point in the history
  • Loading branch information
jheer committed Dec 23, 2020
1 parent 28260d1 commit bde41b2
Show file tree
Hide file tree
Showing 12 changed files with 36 additions and 18 deletions.
4 changes: 2 additions & 2 deletions packages/vega-cli/package.json
@@ -1,6 +1,6 @@
{
"name": "vega-cli",
"version": "5.17.2",
"version": "5.17.3",
"description": "Command line utilities for server-side Vega.",
"keywords": [
"vega",
Expand All @@ -21,7 +21,7 @@
},
"dependencies": {
"canvas": "^2.6.1",
"vega": "5.17.2",
"vega": "5.17.3",
"yargs": "16"
}
}
2 changes: 1 addition & 1 deletion packages/vega-expression/package.json
@@ -1,6 +1,6 @@
{
"name": "vega-expression",
"version": "4.0.0",
"version": "4.0.1",
"description": "Vega expression parser and code generator.",
"keywords": [
"vega",
Expand Down
1 change: 0 additions & 1 deletion packages/vega-expression/src/functions.js
Expand Up @@ -84,7 +84,6 @@ export default function(codegen) {
lower: fn('toLowerCase', STRING, 0),
substring: fn('substring', STRING),
split: fn('split', STRING),
replace: fn('replace', STRING),
trim: fn('trim', STRING, 0),

// REGEXP functions
Expand Down
1 change: 0 additions & 1 deletion packages/vega-expression/test/codegen-test.js
Expand Up @@ -212,7 +212,6 @@ tape('Evaluate expressions with white list', t => {
t.equal(evaluate('trim(" 123 ")'), ' 123 '.trim());
t.equal(evaluate('parseFloat("3.14")'), parseFloat('3.14'));
t.equal(evaluate('parseInt("42")'),parseInt('42'));
t.equal(evaluate('replace("hello world", /hello/, "goodbye")'), 'goodbye world');

// should eval regular expression functions
t.equal(evaluate('test(/ain/, "spain")'), /ain/.test('spain'));
Expand Down
1 change: 1 addition & 0 deletions packages/vega-functions/index.js
Expand Up @@ -58,6 +58,7 @@ export {
indexof,
join,
lastindexof,
replace,
reverse,
slice
} from './src/functions/sequence';
Expand Down
4 changes: 2 additions & 2 deletions packages/vega-functions/package.json
@@ -1,6 +1,6 @@
{
"name": "vega-functions",
"version": "5.9.0",
"version": "5.10.0",
"description": "Custom functions for the Vega expression language.",
"keywords": [
"vega",
Expand All @@ -25,7 +25,7 @@
"d3-color": "^2.0.0",
"d3-geo": "^2.0.1",
"vega-dataflow": "^5.7.3",
"vega-expression": "^4.0.0",
"vega-expression": "^4.0.1",
"vega-scale": "^7.1.1",
"vega-scenegraph": "^4.9.2",
"vega-selections": "^5.1.5",
Expand Down
2 changes: 2 additions & 0 deletions packages/vega-functions/src/codegen.js
Expand Up @@ -142,6 +142,7 @@ import {
indexof,
join,
lastindexof,
replace,
reverse,
slice
} from './functions/sequence';
Expand Down Expand Up @@ -220,6 +221,7 @@ export const functionContext = {
indexof,
join,
lastindexof,
replace,
reverse,
slice,
flush,
Expand Down
6 changes: 5 additions & 1 deletion packages/vega-functions/src/functions/sequence.js
@@ -1,4 +1,4 @@
import {isArray, isString} from 'vega-util';
import {error, isArray, isFunction, isString} from 'vega-util';

function array(seq) {
return isArray(seq) || ArrayBuffer.isView(seq) ? seq : null;
Expand All @@ -24,6 +24,10 @@ export function slice(seq, ...args) {
return sequence(seq).slice(...args);
}

export function replace(str, pattern, repl) {
if (isFunction(repl)) error('Function argument passed to replace.');
return String(str).replace(pattern, repl);
}
export function reverse(seq) {
return array(seq).slice().reverse();
}
15 changes: 14 additions & 1 deletion packages/vega-functions/test/sequence-test.js
@@ -1,5 +1,12 @@
var tape = require('tape'),
{indexof, join, lastindexof, reverse, slice} = require('../');
{
indexof,
join,
lastindexof,
replace,
reverse,
slice
} = require('../');

tape('indexof finds first index', t => {
t.deepEqual(indexof([1, 2, 2, 3], 2), [1, 2, 2, 3].indexOf(2));
Expand All @@ -15,6 +22,12 @@ tape('lastindexof finds last index', t => {
t.end();
});

tape('replace replaces substrings', t => {
t.equal(replace('hello world', /hello/, 'goodbye'), 'goodbye world');
t.throws(() => replace('evil', /.*/, d => d));
t.end();
});

tape('reverse reverses an array', t => {
t.deepEqual(reverse([1, 2, 3]), [1, 2, 3].reverse());
t.throws(() => reverse({reverse: v => v + 1}));
Expand Down
4 changes: 2 additions & 2 deletions packages/vega-parser/package.json
@@ -1,6 +1,6 @@
{
"name": "vega-parser",
"version": "6.1.1",
"version": "6.1.2",
"description": "Parse Vega specifications to runtime dataflows.",
"keywords": [
"vega",
Expand All @@ -24,7 +24,7 @@
"dependencies": {
"vega-dataflow": "^5.7.3",
"vega-event-selector": "^2.0.6",
"vega-functions": "^5.9.0",
"vega-functions": "^5.10.0",
"vega-scale": "^7.1.1",
"vega-util": "^1.15.2"
}
Expand Down
4 changes: 2 additions & 2 deletions packages/vega-view/package.json
@@ -1,6 +1,6 @@
{
"name": "vega-view",
"version": "5.9.1",
"version": "5.9.2",
"description": "View component and transforms for Vega visualizations.",
"keywords": [
"vega",
Expand All @@ -25,7 +25,7 @@
"d3-timer": "^2.0.0",
"vega-dataflow": "^5.7.3",
"vega-format": "^1.0.4",
"vega-functions": "^5.9.0",
"vega-functions": "^5.10.0",
"vega-runtime": "^6.1.3",
"vega-scenegraph": "^4.9.2",
"vega-util": "^1.15.2"
Expand Down
10 changes: 5 additions & 5 deletions packages/vega/package.json
@@ -1,6 +1,6 @@
{
"name": "vega",
"version": "5.17.2",
"version": "5.17.3",
"description": "The Vega visualization grammar.",
"keywords": [
"vega",
Expand Down Expand Up @@ -34,15 +34,15 @@
"vega-dataflow": "~5.7.3",
"vega-encode": "~4.8.3",
"vega-event-selector": "~2.0.6",
"vega-expression": "~4.0.0",
"vega-expression": "~4.0.1",
"vega-force": "~4.0.7",
"vega-format": "~1.0.4",
"vega-functions": "~5.9.0",
"vega-functions": "~5.10.0",
"vega-geo": "~4.3.8",
"vega-hierarchy": "~4.0.9",
"vega-label": "~1.0.0",
"vega-loader": "~4.4.0",
"vega-parser": "~6.1.1",
"vega-parser": "~6.1.2",
"vega-projection": "~1.4.5",
"vega-regression": "~1.0.9",
"vega-runtime": "~6.1.3",
Expand All @@ -53,7 +53,7 @@
"vega-transforms": "~4.9.3",
"vega-typings": "~0.19.2",
"vega-util": "~1.16.0",
"vega-view": "~5.9.1",
"vega-view": "~5.9.2",
"vega-view-transforms": "~4.5.8",
"vega-voronoi": "~4.1.5",
"vega-wordcloud": "~4.1.3"
Expand Down

0 comments on commit bde41b2

Please sign in to comment.