-
Notifications
You must be signed in to change notification settings - Fork 12
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
Substr #10
Changes from 4 commits
7e967ef
d1a9d0b
b21910c
f9a4890
a1f425d
265cd54
394e5b7
842330d
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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); | ||
} | ||
|
||
// Return zero-length string if got oversize number. | ||
if(begin >= strLength){ | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Space needed between |
||
return ""; | ||
} | ||
// Calculating postive version of negative value. | ||
else if(begin < 0) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Since the previous |
||
begin+= strLength; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Space needed between |
||
} | ||
|
||
let end = undefined; | ||
if(typeof len === "undefined"){ | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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") { | ||
|
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); | ||
}); | ||
}); |
There was a problem hiding this comment.
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)