From 2b98b502d6719795ba88a9d7ec2ae3f875641a77 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?B=C3=A1n=20D=C3=A9nes?= Date: Sat, 22 Jan 2022 23:13:18 +0100 Subject: [PATCH] Support string templating for key-level attributes --- src/points.js | 20 +- src/utils.js | 14 ++ test/cli/big/reference/points/points.yaml | 4 +- test/cli/minimal/reference/points/points.yaml | 18 +- test/index.js | 2 +- test/points/adjustments___points.json | 144 ++++++++++- test/points/basic_2x2___points.json | 60 ++++- test/points/default___points.json | 7 +- test/points/overrides___points.json | 224 ++++++++++++++++-- test/unit/utils.js | 13 + 10 files changed, 459 insertions(+), 47 deletions(-) diff --git a/src/points.js b/src/points.js index c4c157f..5f6c0c0 100644 --- a/src/points.js +++ b/src/points.js @@ -72,10 +72,6 @@ const render_zone = exports._render_zone = (zone_name, zone, anchor, global_key, 'object' )() - // propagating object key to name field - - col.name = col_name - // combining row data from zone-wide defs and col-specific defs const actual_rows = Object.keys(prep.extend(zone_wide_rows, col.rows)) @@ -98,7 +94,9 @@ const render_zone = exports._render_zone = (zone_name, zone, anchor, global_key, height: units.$default_height, padding: units.$default_padding, skip: false, - asym: 'both' + asym: 'both', + colrow: '{{col.name}}_{{row}}', + name: '{{zone.name}}_{{colrow}}' } for (const row of actual_rows) { const key = prep.extend( @@ -110,10 +108,11 @@ const render_zone = exports._render_zone = (zone_name, zone, anchor, global_key, col.rows[row] || {} ) - key.name = key.name || `${zone_name}_${col_name}_${row}` + key.zone = zone + key.zone.name = zone_name key.col = col + key.col.name = col_name key.row = row - key.colrow = `${col_name}_${row}` key.stagger = a.sane(key.stagger, `${key.name}.shift`, 'number')(units) key.spread = a.sane(key.spread, `${key.name}.spread`, 'number')(units) @@ -128,6 +127,13 @@ const render_zone = exports._render_zone = (zone_name, zone, anchor, global_key, key.skip = a.sane(key.skip, `${key.name}.skip`, 'boolean')() key.asym = a.in(key.asym, `${key.name}.asym`, ['left', 'right', 'both']) + // templating support + for (const [k, v] of Object.entries(key)) { + if (a.type(v)(units) == 'string') { + key[k] = u.template(v, key) + } + } + keys.push(key) } diff --git a/src/utils.js b/src/utils.js index 9fcb4cb..089f0c0 100644 --- a/src/utils.js +++ b/src/utils.js @@ -18,6 +18,20 @@ const deep = exports.deep = (obj, key, val) => { return obj } +exports.template = (str, vals={}) => { + const regex = /\{\{([^}]*)\}\}/g + let res = str + let shift = 0 + for (const match of str.matchAll(regex)) { + const replacement = deep(vals, match[1]) || '' + res = res.substring(0, match.index + shift) + + replacement + + res.substring(match.index + match[0].length) + shift += replacement.length - match[0].length + } + return res +} + const eq = exports.eq = (a=[], b=[]) => { return a[0] === b[0] && a[1] === b[1] } diff --git a/test/cli/big/reference/points/points.yaml b/test/cli/big/reference/points/points.yaml index bb15b15..82720d6 100644 --- a/test/cli/big/reference/points/points.yaml +++ b/test/cli/big/reference/points/points.yaml @@ -19,10 +19,12 @@ matrix: padding: 19 skip: false asym: both + colrow: default_default name: matrix + zone: + name: matrix col: rows: {} key: {} name: default row: default - colrow: default_default diff --git a/test/cli/minimal/reference/points/points.yaml b/test/cli/minimal/reference/points/points.yaml index 2e296a1..a3179f0 100644 --- a/test/cli/minimal/reference/points/points.yaml +++ b/test/cli/minimal/reference/points/points.yaml @@ -19,10 +19,16 @@ matrix_col_row: padding: 19 skip: false asym: both - name: matrix_col_row - col: - rows: {} - key: {} - name: col - row: row colrow: col_row + name: matrix_col_row + zone: + columns: + col: &ref_0 + rows: {} + key: {} + name: col + rows: + row: {} + name: matrix + col: *ref_0 + row: row diff --git a/test/index.js b/test/index.js index c836545..bf88f30 100644 --- a/test/index.js +++ b/test/index.js @@ -124,7 +124,7 @@ for (let w of cli_what) { const command = read(t, 'command') const output_path = exists(t, 'path') ? read(t, 'path') : 'output' fs.removeSync(output_path) - const version_regex = /\bv\d+\.\d+\.\d+\b/ + const version_regex = /\bv\d+\.\d+\.\d+(\-develop)?\b/ // correct execution if (exists(t, 'log')) { const ref_log = read(t, 'log').replace(version_regex, '') diff --git a/test/points/adjustments___points.json b/test/points/adjustments___points.json index 02b7e60..25ce5c1 100644 --- a/test/points/adjustments___points.json +++ b/test/points/adjustments___points.json @@ -22,14 +22,46 @@ "padding": 19, "skip": false, "asym": "both", + "colrow": "left_bottom", "name": "matrix_left_bottom", + "zone": { + "columns": { + "left": null, + "right": { + "key": { + "stagger": 5, + "spread": 25, + "splay": 5, + "origin": [ + -9, + -9 + ] + }, + "rows": { + "top": { + "orient": -90, + "shift": [ + 0, + 10 + ], + "rotate": 90 + } + }, + "name": "right" + } + }, + "rows": { + "bottom": {}, + "top": {} + }, + "name": "matrix" + }, "col": { "rows": {}, "key": {}, "name": "left" }, - "row": "bottom", - "colrow": "left_bottom" + "row": "bottom" } }, "matrix_left_top": { @@ -55,14 +87,46 @@ "padding": 19, "skip": false, "asym": "both", + "colrow": "left_top", "name": "matrix_left_top", + "zone": { + "columns": { + "left": null, + "right": { + "key": { + "stagger": 5, + "spread": 25, + "splay": 5, + "origin": [ + -9, + -9 + ] + }, + "rows": { + "top": { + "orient": -90, + "shift": [ + 0, + 10 + ], + "rotate": 90 + } + }, + "name": "right" + } + }, + "rows": { + "bottom": {}, + "top": {} + }, + "name": "matrix" + }, "col": { "rows": {}, "key": {}, "name": "left" }, - "row": "top", - "colrow": "left_top" + "row": "top" } }, "matrix_right_bottom": { @@ -88,7 +152,40 @@ "padding": 19, "skip": false, "asym": "both", + "colrow": "right_bottom", "name": "matrix_right_bottom", + "zone": { + "columns": { + "left": null, + "right": { + "key": { + "stagger": 5, + "spread": 25, + "splay": 5, + "origin": [ + -9, + -9 + ] + }, + "rows": { + "top": { + "orient": -90, + "shift": [ + 0, + 10 + ], + "rotate": 90 + } + }, + "name": "right" + } + }, + "rows": { + "bottom": {}, + "top": {} + }, + "name": "matrix" + }, "col": { "key": { "stagger": 5, @@ -111,8 +208,7 @@ }, "name": "right" }, - "row": "bottom", - "colrow": "right_bottom" + "row": "bottom" } }, "matrix_right_top": { @@ -138,7 +234,40 @@ "padding": 19, "skip": false, "asym": "both", + "colrow": "right_top", "name": "matrix_right_top", + "zone": { + "columns": { + "left": null, + "right": { + "key": { + "stagger": 5, + "spread": 25, + "splay": 5, + "origin": [ + -9, + -9 + ] + }, + "rows": { + "top": { + "orient": -90, + "shift": [ + 0, + 10 + ], + "rotate": 90 + } + }, + "name": "right" + } + }, + "rows": { + "bottom": {}, + "top": {} + }, + "name": "matrix" + }, "col": { "key": { "stagger": 5, @@ -161,8 +290,7 @@ }, "name": "right" }, - "row": "top", - "colrow": "right_top" + "row": "top" } } } diff --git a/test/points/basic_2x2___points.json b/test/points/basic_2x2___points.json index 9f717de..d2c7baf 100644 --- a/test/points/basic_2x2___points.json +++ b/test/points/basic_2x2___points.json @@ -22,14 +22,25 @@ "padding": 19, "skip": false, "asym": "both", + "colrow": "left_bottom", "name": "matrix_left_bottom", + "zone": { + "columns": { + "left": null, + "right": null + }, + "rows": { + "bottom": {}, + "top": {} + }, + "name": "matrix" + }, "col": { "rows": {}, "key": {}, "name": "left" }, - "row": "bottom", - "colrow": "left_bottom" + "row": "bottom" } }, "matrix_left_top": { @@ -55,14 +66,25 @@ "padding": 19, "skip": false, "asym": "both", + "colrow": "left_top", "name": "matrix_left_top", + "zone": { + "columns": { + "left": null, + "right": null + }, + "rows": { + "bottom": {}, + "top": {} + }, + "name": "matrix" + }, "col": { "rows": {}, "key": {}, "name": "left" }, - "row": "top", - "colrow": "left_top" + "row": "top" } }, "matrix_right_bottom": { @@ -88,14 +110,25 @@ "padding": 19, "skip": false, "asym": "both", + "colrow": "right_bottom", "name": "matrix_right_bottom", + "zone": { + "columns": { + "left": null, + "right": null + }, + "rows": { + "bottom": {}, + "top": {} + }, + "name": "matrix" + }, "col": { "rows": {}, "key": {}, "name": "right" }, - "row": "bottom", - "colrow": "right_bottom" + "row": "bottom" } }, "matrix_right_top": { @@ -121,14 +154,25 @@ "padding": 19, "skip": false, "asym": "both", + "colrow": "right_top", "name": "matrix_right_top", + "zone": { + "columns": { + "left": null, + "right": null + }, + "rows": { + "bottom": {}, + "top": {} + }, + "name": "matrix" + }, "col": { "rows": {}, "key": {}, "name": "right" }, - "row": "top", - "colrow": "right_top" + "row": "top" } } } diff --git a/test/points/default___points.json b/test/points/default___points.json index a24eb94..d1c34cb 100644 --- a/test/points/default___points.json +++ b/test/points/default___points.json @@ -22,14 +22,17 @@ "padding": 19, "skip": false, "asym": "both", + "colrow": "default_default", "name": "matrix", + "zone": { + "name": "matrix" + }, "col": { "rows": {}, "key": {}, "name": "default" }, - "row": "default", - "colrow": "default_default" + "row": "default" } } } diff --git a/test/points/overrides___points.json b/test/points/overrides___points.json index e1e8152..6edfefd 100644 --- a/test/points/overrides___points.json +++ b/test/points/overrides___points.json @@ -22,14 +22,42 @@ "padding": 19, "skip": false, "asym": "both", + "colrow": "left_bottom", "name": "matrix_left_bottom", + "zone": { + "columns": { + "left": null, + "middle": { + "rows": { + "top": {} + }, + "key": {}, + "name": "middle" + }, + "right": { + "key": { + "stagger": "u" + }, + "rows": { + "bottom": "$unset", + "home": {}, + "top": {} + }, + "name": "right" + } + }, + "rows": { + "bottom": {}, + "home": {} + }, + "name": "matrix" + }, "col": { "rows": {}, "key": {}, "name": "left" }, - "row": "bottom", - "colrow": "left_bottom" + "row": "bottom" } }, "matrix_left_home": { @@ -55,14 +83,42 @@ "padding": 19, "skip": false, "asym": "both", + "colrow": "left_home", "name": "matrix_left_home", + "zone": { + "columns": { + "left": null, + "middle": { + "rows": { + "top": {} + }, + "key": {}, + "name": "middle" + }, + "right": { + "key": { + "stagger": "u" + }, + "rows": { + "bottom": "$unset", + "home": {}, + "top": {} + }, + "name": "right" + } + }, + "rows": { + "bottom": {}, + "home": {} + }, + "name": "matrix" + }, "col": { "rows": {}, "key": {}, "name": "left" }, - "row": "home", - "colrow": "left_home" + "row": "home" } }, "matrix_middle_bottom": { @@ -88,7 +144,36 @@ "padding": 19, "skip": false, "asym": "both", + "colrow": "middle_bottom", "name": "matrix_middle_bottom", + "zone": { + "columns": { + "left": null, + "middle": { + "rows": { + "top": {} + }, + "key": {}, + "name": "middle" + }, + "right": { + "key": { + "stagger": "u" + }, + "rows": { + "bottom": "$unset", + "home": {}, + "top": {} + }, + "name": "right" + } + }, + "rows": { + "bottom": {}, + "home": {} + }, + "name": "matrix" + }, "col": { "rows": { "top": {} @@ -96,8 +181,7 @@ "key": {}, "name": "middle" }, - "row": "bottom", - "colrow": "middle_bottom" + "row": "bottom" } }, "matrix_middle_home": { @@ -123,7 +207,36 @@ "padding": 19, "skip": false, "asym": "both", + "colrow": "middle_home", "name": "matrix_middle_home", + "zone": { + "columns": { + "left": null, + "middle": { + "rows": { + "top": {} + }, + "key": {}, + "name": "middle" + }, + "right": { + "key": { + "stagger": "u" + }, + "rows": { + "bottom": "$unset", + "home": {}, + "top": {} + }, + "name": "right" + } + }, + "rows": { + "bottom": {}, + "home": {} + }, + "name": "matrix" + }, "col": { "rows": { "top": {} @@ -131,8 +244,7 @@ "key": {}, "name": "middle" }, - "row": "home", - "colrow": "middle_home" + "row": "home" } }, "matrix_middle_top": { @@ -158,7 +270,36 @@ "padding": 19, "skip": false, "asym": "both", + "colrow": "middle_top", "name": "matrix_middle_top", + "zone": { + "columns": { + "left": null, + "middle": { + "rows": { + "top": {} + }, + "key": {}, + "name": "middle" + }, + "right": { + "key": { + "stagger": "u" + }, + "rows": { + "bottom": "$unset", + "home": {}, + "top": {} + }, + "name": "right" + } + }, + "rows": { + "bottom": {}, + "home": {} + }, + "name": "matrix" + }, "col": { "rows": { "top": {} @@ -166,8 +307,7 @@ "key": {}, "name": "middle" }, - "row": "top", - "colrow": "middle_top" + "row": "top" } }, "matrix_right_home": { @@ -193,7 +333,36 @@ "padding": 19, "skip": false, "asym": "both", + "colrow": "right_home", "name": "matrix_right_home", + "zone": { + "columns": { + "left": null, + "middle": { + "rows": { + "top": {} + }, + "key": {}, + "name": "middle" + }, + "right": { + "key": { + "stagger": "u" + }, + "rows": { + "bottom": "$unset", + "home": {}, + "top": {} + }, + "name": "right" + } + }, + "rows": { + "bottom": {}, + "home": {} + }, + "name": "matrix" + }, "col": { "key": { "stagger": "u" @@ -205,8 +374,7 @@ }, "name": "right" }, - "row": "home", - "colrow": "right_home" + "row": "home" } }, "matrix_right_top": { @@ -232,7 +400,36 @@ "padding": 19, "skip": false, "asym": "both", + "colrow": "right_top", "name": "matrix_right_top", + "zone": { + "columns": { + "left": null, + "middle": { + "rows": { + "top": {} + }, + "key": {}, + "name": "middle" + }, + "right": { + "key": { + "stagger": "u" + }, + "rows": { + "bottom": "$unset", + "home": {}, + "top": {} + }, + "name": "right" + } + }, + "rows": { + "bottom": {}, + "home": {} + }, + "name": "matrix" + }, "col": { "key": { "stagger": "u" @@ -244,8 +441,7 @@ }, "name": "right" }, - "row": "top", - "colrow": "right_top" + "row": "top" } } } diff --git a/test/unit/utils.js b/test/unit/utils.js index fc3a54b..bade0ee 100644 --- a/test/unit/utils.js +++ b/test/unit/utils.js @@ -22,6 +22,19 @@ describe('Utils', function() { should.equal(u.deep(obj, 'non.existent.key'), undefined) }) + it('template', function() { + u.template('arst').should.equal('arst') + u.template('{arst}}').should.equal('{arst}}') + u.template('{{arst}}').should.equal('') + u.template('{{arst}}', {arst: 'neio'}).should.equal('neio') + u.template('{{a}}_{{b}}', {a: 'c', b: 'd'}).should.equal('c_d') + u.template( + '{{longlonglong}}_{{short}}', + {longlonglong: 'long', short: 'shortshortshort'} + ).should.equal('long_shortshortshort') + u.template('{{a.b.c}}', {a: {b: {c: 'deep'}}}).should.equal('deep') + }) + it('eq', function() { // basic point usage u.eq([1, 2], [1, 2]).should.equal(true)