From 7e967ef7b79ad29d6a6034853bd5cd9bf016f6bc Mon Sep 17 00:00:00 2001 From: nainemom Date: Sat, 29 Apr 2017 19:59:31 +0430 Subject: [PATCH 1/8] Add substr function --- src/index.js | 25 +++++++++++++++++++++++++ 1 file changed, 25 insertions(+) diff --git a/src/index.js b/src/index.js index c00bb63..3a184c7 100644 --- a/src/index.js +++ b/src/index.js @@ -28,6 +28,31 @@ export function substring(str, begin = 0, end) { return str.match(astralRange).slice(begin, end).join(""); } +export function substr(str, begin = 0, len) { + // Check for input + if (typeof str !== "string") { + throw new Error("Input must be a string"); + } + + if (typeof begin !== "number") { + begin = 0; + } + // Calculating postive version of negative value. + else if(begin < 0) { + begin+= length(str); + } + + let end = undefined; + if (typeof len === "number" && len >= 0) { + end = len + begin; + } + else if(typeof len === "number" && len < 0){ + end = 0; + } + + return str.match(astralRange).slice(begin, end).join(""); +} + export function limit(str, limit = 16, padString = "#", padPosition = "right") { // Input should be a string, limit should be a number if (typeof str !== "string" || typeof limit !== "number") { From d1a9d0bb27e6d152e3613261edff57d698290c03 Mon Sep 17 00:00:00 2001 From: nainemom Date: Sat, 29 Apr 2017 19:59:55 +0430 Subject: [PATCH 2/8] Add test for substr function --- test/substr.test.js | 32 ++++++++++++++++++++++++++++++++ 1 file changed, 32 insertions(+) create mode 100644 test/substr.test.js diff --git a/test/substr.test.js b/test/substr.test.js new file mode 100644 index 0000000..f966fc6 --- /dev/null +++ b/test/substr.test.js @@ -0,0 +1,32 @@ +import assert from "assert"; +import { substr } from "../src/index"; + +describe("Substr", () => { + const string = "Two things are infinite: the universe and human stupidity; and I'm not sure about the universe."; + const unicodeString = "دانشگاهی که دانشگاه نباشد، دانشگاه نیست."; + const emojiString = "Emojis 👍🏽 are 🍆 poison. 🌮s are bad."; + + it("Substrs latin text correctly", () => { + assert.strictEqual(substr("Iñtërnâtiônàlizætiøn☃", 0, 10), "Iñtërnâtiô"); + assert.strictEqual(substr(string, 25, 32), "the universe and human stupidity"); + }); + + it("Substrs unicode text correctly", () => { + assert.strictEqual(substr(unicodeString, 0, 11), "دانشگاهی که"); + assert.strictEqual(substr(emojiString, 7, 7), "👍🏽 are 🍆"); + }); + + it("Substrs if arguments are unspecified", () => { + assert.strictEqual(substr(string, 10), string.substr(10)); + assert.strictEqual(substr(string), string); + }); + + it("Substrs even with negative arguments", () => { + assert.strictEqual(substr(string, 0, -10), string.substr(0, -10)); + assert.strictEqual(substr(string, -10, -10), string.substr(-10, -10)); + }); + + it("Throws an error if wrong arguments are specified.", () => { + assert.throws(() => substr(12, 1, 2), Error); + }); +}); From b21910cccdcd3cc874c81c22179041da99d77ce4 Mon Sep 17 00:00:00 2001 From: nainemom Date: Sun, 30 Apr 2017 15:16:44 +0430 Subject: [PATCH 3/8] check & fix inputs just like original substr --- src/index.js | 29 +++++++++++++++++++++++------ 1 file changed, 23 insertions(+), 6 deletions(-) diff --git a/src/index.js b/src/index.js index 3a184c7..c2fe7ef 100644 --- a/src/index.js +++ b/src/index.js @@ -34,20 +34,37 @@ export function substr(str, begin = 0, len) { throw new Error("Input must be a string"); } + const strLength = length(str); + + // Fix type if (typeof begin !== "number") { - begin = 0; + begin = parseInt(begin); + } + + // Return zero-length string if got oversize number. + if(begin >= strLength){ + return ""; } // Calculating postive version of negative value. else if(begin < 0) { - begin+= length(str); + begin+= strLength; } let end = undefined; - if (typeof len === "number" && len >= 0) { - end = len + begin; + if(typeof len === "undefined"){ + end = strLength; } - else if(typeof len === "number" && len < 0){ - end = 0; + else { + // Fix type + if (typeof len !== "number"){ + len = parseInt(len); + } + if (len >= 0) { + end = len + begin; + } + else{ + end = begin; + } } return str.match(astralRange).slice(begin, end).join(""); From f9a489069835d01526b6abde37c7db10a799ffe4 Mon Sep 17 00:00:00 2001 From: nainemom Date: Sun, 30 Apr 2017 15:16:54 +0430 Subject: [PATCH 4/8] add more tests --- test/substr.test.js | 8 +++++--- 1 file changed, 5 insertions(+), 3 deletions(-) diff --git a/test/substr.test.js b/test/substr.test.js index f966fc6..970c7c8 100644 --- a/test/substr.test.js +++ b/test/substr.test.js @@ -8,7 +8,7 @@ describe("Substr", () => { it("Substrs latin text correctly", () => { assert.strictEqual(substr("Iñtërnâtiônàlizætiøn☃", 0, 10), "Iñtërnâtiô"); - assert.strictEqual(substr(string, 25, 32), "the universe and human stupidity"); + assert.strictEqual(substr(string, 25, 32), string.substr(25, 32)); }); it("Substrs unicode text correctly", () => { @@ -18,12 +18,14 @@ describe("Substr", () => { it("Substrs if arguments are unspecified", () => { assert.strictEqual(substr(string, 10), string.substr(10)); + assert.strictEqual(substr(string, 120), string.substr(120)); + assert.strictEqual(substr(string, '1', '2'), string.substr('1', '2')); assert.strictEqual(substr(string), string); }); it("Substrs even with negative arguments", () => { - assert.strictEqual(substr(string, 0, -10), string.substr(0, -10)); - assert.strictEqual(substr(string, -10, -10), string.substr(-10, -10)); + assert.strictEqual(substr(string, -4), string.substr(-4)); + assert.strictEqual(substr(string, -4, -1), string.substr(-4, -1)); }); it("Throws an error if wrong arguments are specified.", () => { From a1f425dd68d4b1380df20e79cfd51c9529d41b38 Mon Sep 17 00:00:00 2001 From: nainemom Date: Sun, 30 Apr 2017 16:15:48 +0430 Subject: [PATCH 5/8] formatting --- src/index.js | 20 +++++++++++--------- 1 file changed, 11 insertions(+), 9 deletions(-) diff --git a/src/index.js b/src/index.js index c2fe7ef..13331d0 100644 --- a/src/index.js +++ b/src/index.js @@ -42,28 +42,30 @@ export function substr(str, begin = 0, len) { } // Return zero-length string if got oversize number. - if(begin >= strLength){ + if (begin >= strLength) { return ""; } + // Calculating postive version of negative value. - else if(begin < 0) { - begin+= strLength; + if (begin < 0) { + begin += strLength; } - let end = undefined; - if(typeof len === "undefined"){ + let end; + + if (typeof len === "undefined") { end = strLength; } else { // Fix type - if (typeof len !== "number"){ - len = parseInt(len); + if (typeof len !== "number") { + len = parseInt(len, 10); } if (len >= 0) { end = len + begin; } - else{ - end = begin; + else { + end= begin; } } From 265cd54a5f9f29f340b56f79473b3edf2d66d1b6 Mon Sep 17 00:00:00 2001 From: nainemom Date: Sun, 30 Apr 2017 16:16:01 +0430 Subject: [PATCH 6/8] add substr function to description --- README.md | 13 +++++++++++++ 1 file changed, 13 insertions(+) diff --git a/README.md b/README.md index d5bc242..a1e6a71 100644 --- a/README.md +++ b/README.md @@ -86,6 +86,19 @@ length("Iñtërnâtiônàlizætiøn☃💩"); // 22 ```javascript substring("Emojis 👍🏽 are 🍆 poison. 🌮s are bad.", 7, 14); // "👍🏽 are 🍆" ``` +### Substr + function substr(str[, start[, end]]) + +| Param | Type | Default | Description | +|---|---|---|---| +| str | String | *none* | String to be devided | +| start | Number | Start of string | Start position | +| end | Number | String length minus `start` parameter | Length of result | + +#### Examples +```javascript +substr("A.C. Milan 🇮🇹⚽️", 5, 7); // "Milan 🇮🇹" +``` ## Test ```bash From 394e5b7c51596478bee71db1c2628e06a8c8d408 Mon Sep 17 00:00:00 2001 From: nainemom Date: Sun, 30 Apr 2017 16:28:58 +0430 Subject: [PATCH 7/8] add radix of 10 to parseInt --- src/index.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/index.js b/src/index.js index 13331d0..ee2b049 100644 --- a/src/index.js +++ b/src/index.js @@ -38,7 +38,7 @@ export function substr(str, begin = 0, len) { // Fix type if (typeof begin !== "number") { - begin = parseInt(begin); + begin = parseInt(begin, 10); } // Return zero-length string if got oversize number. From 842330dedd6e227eb5b85648d565d74046576641 Mon Sep 17 00:00:00 2001 From: nainemom Date: Sun, 30 Apr 2017 16:35:40 +0430 Subject: [PATCH 8/8] add substr function to features section --- README.md | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/README.md b/README.md index a1e6a71..d7aaf88 100644 --- a/README.md +++ b/README.md @@ -14,6 +14,7 @@ wrong (hint: it should be 1!). ES6's `Array.from` tried to solve this, but that - Limit string to width (truncate/pad) - Unicode-aware string length - Unicode-aware substring +- Unicode-aware substr 🔥 Please note that this library is built for accuracy, not performance. It uses complex regular expressions to calculate the string length and perform other operations which are **not** particularly super-jawdropping-fast like @@ -29,7 +30,7 @@ And import it in your awesome node app: ```javascript // ES2015+ import * as stringz from 'stringz'; // OR: -import { limit, substring, length } from 'stringz'; +import { limit, substring, length, substr } from 'stringz'; // CommonJS var stringz = require('stringz');