Skip to content

Commit

Permalink
add stepper() function and refactor exist range() function for global…
Browse files Browse the repository at this point in the history
… tags
  • Loading branch information
goosy committed Nov 16, 2024
1 parent 75cb440 commit 49e83ff
Show file tree
Hide file tree
Showing 4 changed files with 39 additions and 25 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "gooconverter",
"version": "1.0.1",
"version": "1.0.3",
"type": "module",
"description": "gen string from template",
"main": "lib/index.cjs",
Expand Down
7 changes: 4 additions & 3 deletions readme.MD
Original file line number Diff line number Diff line change
Expand Up @@ -16,11 +16,12 @@

`{{表达式}}`

在表达式中还可以使用转换器的唯一内置函数:
在表达式中还可以使用转换器的内置函数:

`range(start, end, step)`
- `range(start, end, step)` 产生一个递增或递减的生成器对象,从 start 开始,小于end,步长为 step。
- `stepper(start, step)` 产生一个递增或递减的步进器,s.next() 步进到下一个值并返回, s.value 为当前值。

参数为整数,输出一个从 start 开始,小于end,步长为 step 的数组
非整数时,请注意输出为浮点数,建议输出时用 toFixed(n) 进行格式化

### 赋值:

Expand Down
35 changes: 20 additions & 15 deletions src/converter.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,7 @@
* @typedef {Rule[]} Rules Conversion rule table
*/

import {
parseToDOM
} from "./gooparse.js";
import { parseToDOM } from "./gooparse.js";

/**
* @param {Tags} tags tags dict for template
Expand Down Expand Up @@ -40,22 +38,28 @@ function parseMemberExpression(tags, es_expression) {
}

const global_tags = {
range(...argus) {
*range(...argus) {
let [start, end, step] = argus;
if (start === undefined) return [];
if (start === undefined) return;
if (end === undefined) {
end = start
start = 0
}
const direct = start < end;
step ??= direct ? 1 : -1;
const ret = [];
let index = start;
while (direct ? index < end : index > end) {
ret.push(index);
yield index;
index += step;
}
return ret;
},
stepper(init_value = 0, step = 1) {
let value = init_value;
const next = () => {
value += step;
return value;
}
return { value, next }
},
Object, Array, Map, Set, // Transparently transmit some system objects
}
Expand Down Expand Up @@ -258,17 +262,18 @@ function computeESExpression(tags, es_expression) {
function convert_FOR_Goonode(tags, node) {
let key;
let value;
let list;
let iterable;
let content = '';
const left = node.expression.left;
const right = computeESExpression(tags, node.expression.right);
if (!right) throw Error("wrong for statement!");
const isArray = Array.isArray(right);
const is_array = Array.isArray(right);
const is_iterable = right != null && typeof right[Symbol.iterator] === 'function';
// {{for v in object}}
if (left.type === 'Identifier') {
value = left.name;
list = Object.values(right);
for (const item of list) {
iterable = is_iterable ? right : Object.values(right);
for (const item of iterable) {
content += convert_dom({
...tags,
[value]: item
Expand All @@ -280,9 +285,9 @@ function convert_FOR_Goonode(tags, node) {
if (left.type === "ArrayExpression") {
key = left[0].name;
value = left[1].name;
list = Object.entries(right);
for (let [k, v] of list) {
k = isArray ? Number.parseInt(k) : k;
iterable = Object.entries(is_iterable ? [...right] : right);
for (let [k, v] of iterable) {
k = is_array ? Number.parseInt(k) : k;
content += convert_dom({
...tags,
[key]: k,
Expand Down
20 changes: 14 additions & 6 deletions test/test.js
Original file line number Diff line number Diff line change
Expand Up @@ -106,14 +106,22 @@ suite('convert(tags, template)', () => {
);
});
test('call outputs correctly', () => {
strictEqual( // {{ range() }} The output is correct
strictEqual( // {{ range() }} outputs correctly
convert(
{},
'{{range(10)// array}}\n{{for i in range(1,11)}}{{i}} {{endfor}}\n{{for i in range(10,-1,-2)}}{{i}} {{endfor}}'
'{{[...range(10)]// array}}\n{{for i in range(1,11)}}{{i}} {{endfor}}\n{{for i in range(10,8,-0.2)}}{{i.toFixed(1)}} {{endfor}}'
),
"0,1,2,3,4,5,6,7,8,9\n1 2 3 4 5 6 7 8 9 10 \n10 8 6 4 2 0 "
"0,1,2,3,4,5,6,7,8,9\n1 2 3 4 5 6 7 8 9 10 \n10.0 9.8 9.6 9.4 9.2 9.0 8.8 8.6 8.4 8.2 8.0 "
);
strictEqual( // Object.valueOf Object.entries The output is correct'
strictEqual( // {{ stepper() }} outputs correctly
convert(
{},
'{{s = stepper(10,2)}}{{for i in range(1,11)}} {{s.next()}}{{endfor}}\n'
+ '{{s = stepper(0,0.1)}}{{for i in range(0,10)}} {{s.next().toFixed(1)}}{{endfor}}'
),
" 12 14 16 18 20 22 24 26 28 30\n 0.1 0.2 0.3 0.4 0.5 0.6 0.7 0.8 0.9 1.0"
);
strictEqual( // Object.valueOf and Object.entries output correctly
convert(
{ 'myobj': { name: 'myobj', value: 'v' } },
'{{myobj.valueOf()}} {{Object.entries(myobj)}}'
Expand All @@ -134,7 +142,7 @@ suite('convert(tags, template)', () => {
),
"3\n123"
);
strictEqual( // Custom method output is correct
strictEqual( // dustom method outputs correctly
convert(
{
show() {
Expand Down Expand Up @@ -213,7 +221,7 @@ suite('convert(tags, template)', () => {
);
strictEqual(
convert(
{list: [1, 2, 3]},
{ list: [1, 2, 3] },
'{{\n \n// comment\nfor i in list//}}{{i}}{{endfor}}'),
"123"
);
Expand Down

0 comments on commit 49e83ff

Please sign in to comment.