Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Substr #10

Merged
merged 8 commits into from
Apr 30, 2017
Merged
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
42 changes: 42 additions & 0 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,48 @@ 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");
}

const strLength = length(str);

// Fix type
if (typeof begin !== "number") {
begin = parseInt(begin);
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

parseInt(begin) needs to have a radix of 10. parseInt(begin, 10)

}

// Return zero-length string if got oversize number.
if(begin >= strLength){
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Space needed between if and (;

return "";
}
// Calculating postive version of negative value.
else if(begin < 0) {
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Since the previous if returns something, this doesn't need to be else if and a simple if is enough. Also space needed between if and (. Please use ESLint to see all the styling issues.

begin+= strLength;
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Space needed between begin and +=

}

let end = undefined;
if(typeof len === "undefined"){
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This whole section needs a bit of reformatting. suggestion:

let end;

if (typeof len === "undefined"){
    end = strLength;
} else {
    // Fix type
    if (typeof len !== "number"){
        len = parseInt(len, 10);
    }

    if (len >= 0) {
        end = len + begin;
    } else {
        end = begin;
    }
}

end = strLength;
}
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("");
}

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") {
Expand Down
34 changes: 34 additions & 0 deletions test/substr.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
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), string.substr(25, 32));
});

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, 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, -4), string.substr(-4));
assert.strictEqual(substr(string, -4, -1), string.substr(-4, -1));
});

it("Throws an error if wrong arguments are specified.", () => {
assert.throws(() => substr(12, 1, 2), Error);
});
});