Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- export let re2 = /^(?:([-+]?(?:\d+|\d+\.\d+))\s*,\s*)?([-+]?(?:\d+|\d+\.\d+))(?:\s*\.\.(\.?)\s*([-+]?(?:\d+|\d+\.\d+)))?$/;
- export let re = /^([-+]?(?:\d+|\d+\.\d+))(?:\s*,\s*([-+]?(?:\d+|\d+\.\d+)))?(?:\s*(\.\.\.?)\s*([-+]?(?:\d+|\d+\.\d+)))?$/;
- function toArray(value) {
- if (Array.isArray(value)) {
- return value.slice();
- }
- if (typeof value !== "string") {
- return [ value ];
- }
- var matched = re.exec(value.trim());
- if (!matched) {
- return [];
- }
- let [ _, first, second, dots, last ] = matched;
- first = +first;
- if (typeof last === "undefined") {
- return [ first ];
- }
- last = +last;
- let a = [];
- let step = (typeof second !== "undefined") ? (+second - first) : (first < last) ? 1 : -1;
- // console.log(first, step, last);
- let x = first;
- if (dots === "...") {
- if (0 < step) {
- while (x < last) {
- a.push(x);
- x += step;
- }
- } else if (step < 0) {
- while (x > last) {
- a.push(x);
- x += step;
- }
- }
- } else {
- if (0 < step) {
- while (x <= last) {
- a.push(x);
- x += step;
- }
- } else if (step < 0) {
- while (x >= last) {
- a.push(x);
- x += step;
- }
- }
- }
- return a;
- }
- function test(str) {
- let result = toArray(str);
- console.log(str, result);
- // let matched = re.exec(str);
- //
- // if (!matched) {
- // return;
- // }
- //
- // let [ _, first, second, dots, last ] = matched;
- //
- // console.log(`first: ${first}`);
- // console.log(`second: ${second}`);
- // console.log(`last: ${last}`);
- // console.log(`dots: ${dots}`);
- // console.log("=======================");
- }
- test("1");
- test("1..20");
- test("1...20");
- test("0, 2..20");
- test("0, 2...20");
- test("0, -2..20");
- test("0, 0..20");
- test("0, 1");
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement