Skip to content

Commit f9bd1fc

Browse files
authored
Update Function declarations in example
1 parent bd9b7de commit f9bd1fc

File tree

1 file changed

+29
-29
lines changed

1 file changed

+29
-29
lines changed

README.md

Lines changed: 29 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -70,7 +70,7 @@ SELECT js_create_scalar('function_name', 'function_code');
7070

7171
```sql
7272
-- Create a custom function to calculate age from birth date
73-
SELECT js_create_scalar('age', 'function(args) {
73+
SELECT js_create_scalar('age', '(function(args) {
7474
const birthDate = new Date(args[0]);
7575
const today = new Date();
7676
let age = today.getFullYear() - birthDate.getFullYear();
@@ -79,7 +79,7 @@ SELECT js_create_scalar('age', 'function(args) {
7979
age--;
8080
}
8181
return age;
82-
}');
82+
})');
8383

8484
-- Use the function
8585
SELECT name, age(birth_date) FROM people;
@@ -111,20 +111,20 @@ SELECT js_create_aggregate('median',
111111
'values = [];',
112112

113113
-- Step code: collect values from each row
114-
'function(args) {
114+
'(function(args) {
115115
values.push(args[0]);
116-
}',
116+
})',
117117

118118
-- Final code: calculate the median
119-
'function() {
119+
'(function() {
120120
values.sort((a, b) => a - b);
121121
const mid = Math.floor(values.length / 2);
122122
if (values.length % 2 === 0) {
123123
return (values[mid-1] + values[mid]) / 2;
124124
} else {
125125
return values[mid];
126126
}
127-
}'
127+
})'
128128
);
129129

130130
-- Use the function
@@ -159,24 +159,24 @@ SELECT js_create_window('moving_avg',
159159
'sum = 0; count = 0;',
160160

161161
-- Step code: process each row
162-
'function(args) {
162+
'(function(args) {
163163
sum += args[0];
164164
count++;
165-
}',
165+
})',
166166

167167
-- Final code: not needed for this example
168-
'function() { }',
168+
'(function() { })',
169169

170170
-- Value code: return current average
171-
'function() {
171+
'(function() {
172172
return count > 0 ? sum / count : null;
173-
}',
173+
})',
174174

175175
-- Inverse code: remove a value from the window
176-
'function(args) {
176+
'(function(args) {
177177
sum -= args[0];
178178
count--;
179-
}'
179+
})'
180180
);
181181

182182
-- Use the function
@@ -203,7 +203,7 @@ SELECT js_create_collation('collation_name', 'collation_function');
203203

204204
```sql
205205
-- Create a case-insensitive natural sort collation
206-
SELECT js_create_collation('natural_nocase', 'function(a, b) {
206+
SELECT js_create_collation('natural_nocase', '(function(a, b) {
207207
// Extract numbers for natural comparison
208208
const splitA = a.toLowerCase().split(/(\d+)/);
209209
const splitB = b.toLowerCase().split(/(\d+)/);
@@ -217,7 +217,7 @@ SELECT js_create_collation('natural_nocase', 'function(a, b) {
217217
}
218218
}
219219
return splitA.length - splitB.length;
220-
}');
220+
})');
221221

222222
-- Use the collation
223223
SELECT * FROM files ORDER BY name COLLATE natural_nocase;
@@ -263,10 +263,10 @@ SELECT js_eval('new Date(1629381600000).toLocaleDateString()');
263263

264264
```sql
265265
-- Create a function to extract domain from email
266-
SELECT js_create_scalar('get_domain', 'function(args) {
266+
SELECT js_create_scalar('get_domain', '(function(args) {
267267
const email = args[0];
268268
return email.split("@")[1] || null;
269-
}');
269+
})');
270270

271271
-- Use it in a query
272272
SELECT email, get_domain(email) AS domain FROM users;
@@ -279,18 +279,18 @@ SELECT email, get_domain(email) AS domain FROM users;
279279
SELECT js_create_aggregate('stddev',
280280
'sum = 0; sumSq = 0; count = 0;',
281281

282-
'function(args) {
282+
'(function(args) {
283283
const val = args[0];
284284
sum += val;
285285
sumSq += val * val;
286286
count++;
287-
}',
287+
})',
288288

289-
'function() {
289+
'(function() {
290290
if (count < 2) return null;
291291
const variance = (sumSq - (sum * sum) / count) / (count - 1);
292292
return Math.sqrt(variance);
293-
}'
293+
})'
294294
);
295295

296296
-- Use it in a query
@@ -304,26 +304,26 @@ SELECT department, stddev(salary) FROM employees GROUP BY department;
304304
SELECT js_create_window('percentile_rank',
305305
'values = [];',
306306

307-
'function(args) {
307+
'(function(args) {
308308
values.push(args[0]);
309-
}',
309+
})',
310310

311-
'function() {
311+
'(function() {
312312
values.sort((a, b) => a - b);
313-
}',
313+
})',
314314

315-
'function() {
315+
'(function() {
316316
const current = values[values.length - 1];
317317
const rank = values.indexOf(current);
318318
return (rank / (values.length - 1)) * 100;
319-
}',
319+
})',
320320

321-
'function(args) {
321+
'(function(args) {
322322
const index = values.indexOf(args[0]);
323323
if (index !== -1) {
324324
values.splice(index, 1);
325325
}
326-
}'
326+
})'
327327
);
328328

329329
-- Use it in a query

0 commit comments

Comments
 (0)