forked from agalwood/Motrix
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
1 changed file
with
68 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,68 @@ | ||
const RULE_REGEX = /\(([^)]*)\)/ | ||
const OPERATOR_PLUS = '+' | ||
const OPERATOR_MINUS = '-' | ||
|
||
export function getRuleString (out) { | ||
const rule = out.match(RULE_REGEX) | ||
const result = rule && rule[1] | ||
|
||
return result | ||
} | ||
|
||
export function buildRule (rule) { | ||
let ruleArr | ||
let operator | ||
let init = 1 | ||
let step = 1 | ||
let len = 1 | ||
|
||
if (rule.includes('+')) { | ||
ruleArr = rule.split('+') | ||
operator = OPERATOR_PLUS | ||
} else if (rule.includes('-')) { | ||
ruleArr = rule.split('-') | ||
operator = OPERATOR_MINUS | ||
} | ||
|
||
if (ruleArr) { | ||
len = ruleArr[0].length | ||
init = parseInt(ruleArr[0], 10) | ||
step = ruleArr[1] || 1 | ||
if (operator === OPERATOR_MINUS) { | ||
step = -step | ||
} | ||
} | ||
|
||
return { | ||
init, | ||
step, | ||
len | ||
} | ||
} | ||
|
||
export function buildOuts (uris = [], out = '') { | ||
const result = [] | ||
const count = uris.length | ||
if (count === 0 || !out) { | ||
return result | ||
} | ||
|
||
const ruleStr = getRuleString(out) | ||
if (!ruleStr) { | ||
return result | ||
} | ||
const rule = buildRule(ruleStr) | ||
|
||
let idx | ||
let temp | ||
|
||
for (let i = 0; i < count; i++) { | ||
idx = `${rule.init + rule.step * i}`.padStart(rule.len, '0') | ||
|
||
temp = out.replace(RULE_REGEX, idx) | ||
|
||
result.push(temp) | ||
} | ||
|
||
return result | ||
} |