From 817fec6238465d2ecc8e1490fa9dfafdf8f86e44 Mon Sep 17 00:00:00 2001 From: "copilot-swe-agent[bot]" <198982749+Copilot@users.noreply.github.com> Date: Sun, 30 Nov 2025 12:10:05 +0000 Subject: [PATCH 1/2] Initial plan From 0cf0180202523186bfb99f56c19fdc8b4e28b915 Mon Sep 17 00:00:00 2001 From: "copilot-swe-agent[bot]" <198982749+Copilot@users.noreply.github.com> Date: Sun, 30 Nov 2025 12:14:43 +0000 Subject: [PATCH 2/2] Fix REPLACE function to convert all parameters to string and update tests Co-authored-by: mathiasrw <1063454+mathiasrw@users.noreply.github.com> --- src/55functions.js | 4 +++- test/test1455.js | 57 ++++++++++++++++++---------------------------- 2 files changed, 25 insertions(+), 36 deletions(-) diff --git a/src/55functions.js b/src/55functions.js index 09d3a1efd9..d3f893bb7c 100644 --- a/src/55functions.js +++ b/src/55functions.js @@ -454,7 +454,9 @@ Object.keys(alasql._aggrOriginal).forEach(function (k) { // String functions stdfn.REPLACE = function (target, pattern, replacement) { - return (target.toString() || '').split(pattern).join(replacement); + return (target == null ? '' : target.toString()) + .split(pattern == null ? '' : pattern.toString()) + .join(replacement == null ? '' : replacement.toString()); }; // This array is required for fast GUID generation diff --git a/test/test1455.js b/test/test1455.js index 2a2cb1f6b6..324957c204 100644 --- a/test/test1455.js +++ b/test/test1455.js @@ -3,49 +3,36 @@ if (typeof exports === 'object') { var alasql = require('..'); } -describe('Test 000 - multiple statements', function () { - const test = '000'; // insert test file number +describe('Test 1455 - REPLACE function', function () { + it('A) REPLACE with string target', function () { + var res = alasql("SELECT REPLACE('123', '2', '_') AS result"); + assert.deepEqual(res, [{result: '1_3'}]); + }); - before(function () { - alasql('create database test' + test); - alasql('use test' + test); + it('B) REPLACE with number target', function () { + var res = alasql("SELECT REPLACE(123, '2', '_') AS result"); + assert.deepEqual(res, [{result: '1_3'}]); }); - after(function () { - alasql('drop database test' + test); + it('C) REPLACE with string and number returning same result', function () { + var res = alasql( + "SELECT REPLACE('123', '2', '_') AS str_result, REPLACE(123, '2', '_') AS num_result" + ); + assert.deepEqual(res, [{str_result: '1_3', num_result: '1_3'}]); }); - it('A) From single lines', function () { - var res = []; - res.push(alasql('create table one (a int)')); - res.push(alasql('insert into one values (1),(2),(3),(4),(5)')); - res.push(alasql('select * from one')); - assert.deepEqual(res, [1, 5, [{a: 1}, {a: 2}, {a: 3}, {a: 4}, {a: 5}]]); + it('D) REPLACE with number in pattern', function () { + var res = alasql("SELECT REPLACE('123', 2, '_') AS result"); + assert.deepEqual(res, [{result: '1_3'}]); }); - it('B) Multiple statements in one string', function () { - // - var sql = 'create table two (a int);'; - sql += 'insert into two values (1),(2),(3),(4),(5);'; - sql += 'select * from two;'; - var res = alasql(sql); - assert.deepEqual(res, [1, 5, [{a: 1}, {a: 2}, {a: 3}, {a: 4}, {a: 5}]]); + it('E) REPLACE with number in replacement', function () { + var res = alasql("SELECT REPLACE('1_3', '_', 2) AS result"); + assert.deepEqual(res, [{result: '123'}]); }); - it('C) Multiple statements in one string with callback', function (done) { - // Please note that first parameter (here `done`) must be called if defined - and is needed when testing async code - var sql = 'create table three (a int);'; - sql += 'insert into three values (1),(2),(3),(4),(5);'; - sql += 'select * from three;'; - alasql(sql, function (res) { - assert.deepEqual(res, [1, 5, [{a: 1}, {a: 2}, {a: 3}, {a: 4}, {a: 5}]]); - done(); - }); + it('F) REPLACE with all numbers', function () { + var res = alasql('SELECT REPLACE(123, 2, 9) AS result'); + assert.deepEqual(res, [{result: '193'}]); }); }); -stdfn.REPLACE = function (target, pattern, replacement) { - if (typeof target !== 'string') { - throw new TypeError('Target must be a string'); - } - return target.split(pattern).join(replacement); -}; \ No newline at end of file