Skip to content
This repository was archived by the owner on Aug 22, 2019. It is now read-only.

Commit 0bf74d1

Browse files
authored
Merge pull request #1 from ramya-rao-a/add-offset-numbering
Support offset when numbering
2 parents f6e3014 + 84efd7b commit 0bf74d1

3 files changed

Lines changed: 19 additions & 4 deletions

File tree

lib/numbering.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -103,8 +103,8 @@ function getNumberingRanges(str) {
103103
* @return {String}
104104
*/
105105
function replaceNumberingRanges(str, ranges, value) {
106-
const replaced = replaceRanges(str, ranges, token => {
107-
let _value = String(value);
106+
const replaced = replaceRanges(str, ranges, (token, offset) => {
107+
let _value = String(value + offset);
108108
// pad values for multiple numbering tokens, e.g. 3 for $$$ becomes 003
109109
while (_value.length < token.length) {
110110
_value = '0' + _value;

lib/utils.js

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -45,9 +45,19 @@ export function replaceRanges(str, ranges, value) {
4545
for (let i = ranges.length - 1; i >= 0; i--) {
4646
const r = ranges[i];
4747

48+
let offset = 0;
49+
let offsetLength = 0;
50+
if (str.substr(r[0] + r[1], 1) === '@'){
51+
const matches = str.substr(r[0] + r[1] + 1).match(/^(\d+)/);
52+
if (matches) {
53+
offsetLength = matches[1].length + 1;
54+
offset = parseInt(matches[1]) - 1;
55+
}
56+
}
57+
4858
str = str.substring(0, r[0])
49-
+ (typeof value === 'function' ? value(str.substr(r[0], r[1])) : value)
50-
+ str.substring(r[0] + r[1]);
59+
+ (typeof value === 'function' ? value(str.substr(r[0], r[1]), offset) : value)
60+
+ str.substring(r[0] + r[1] + offsetLength);
5161
}
5262

5363
return str;

test/numbering.js

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,4 +41,9 @@ describe('Item numbering', () => {
4141
const tree = numbering(parse('[!foo="bar$"]*2'));
4242
assert(tree.firstChild.getAttribute('foo').options.implied);
4343
});
44+
45+
it('add offset to numbering', () => {
46+
assert.equal(expand('span.item$@3*2'), '<span class="item3"></span><span class="item4"></span>');
47+
assert.equal(expand('span.item$$@3*2'), '<span class="item03"></span><span class="item04"></span>');
48+
});
4449
});

0 commit comments

Comments
 (0)