string-tool
Advanced tools
Comparing version 1.2.0 to 1.2.1
{ | ||
"name": "string-tool", | ||
"version": "1.2.0", | ||
"version": "1.2.1", | ||
"description": "Useful functions for handling Strings", | ||
"main": "string-tool.es5.js", | ||
"scripts": { | ||
"test": "make test" | ||
}, | ||
"keywords": [ | ||
@@ -14,2 +10,7 @@ "string" | ||
"license": "GPLv3", | ||
"repository": "JannesMeyer/string-tool", | ||
"scripts": { | ||
"test": "make test" | ||
}, | ||
"main": "string-tool.es5.js", | ||
"devDependencies": { | ||
@@ -16,0 +17,0 @@ "babel": "x", |
@@ -1,6 +0,5 @@ | ||
import { startsWith, capitalize } from '../string-tool.es5'; | ||
import { startsWith, capitalize, nthIndexOf, cutFirst, cutFromIndex } from '../'; | ||
describe('startsWith', () => { | ||
it('should return true when the string starts with another string', () => { | ||
describe("startsWith", () => { | ||
it("should return true when the string starts with another string", () => { | ||
expect(startsWith('A test', 'A t')).toBe(true); | ||
@@ -10,11 +9,9 @@ expect(startsWith('A test', 'A test')).toBe(true); | ||
it('should return false when the string starts with something else', () => { | ||
it("should return false when the string starts with something else", () => { | ||
expect(startsWith('something', 'nothing')).toBe(false); | ||
}); | ||
}); | ||
describe('capitalize', () => { | ||
it('should return true when the first letter was capitalized correctly', () => { | ||
describe("capitalize", () => { | ||
it("should return true when the first letter was capitalized correctly", () => { | ||
expect(capitalize('a')).toEqual('A'); | ||
@@ -26,6 +23,15 @@ expect(capitalize('ß')).toEqual('SS'); | ||
it('should return an empty string when the input is empty', () => { | ||
it("should return an empty string when the input is empty", () => { | ||
expect(capitalize('')).toEqual(''); | ||
}); | ||
}); | ||
describe("nthIndexOf", () => { | ||
it("should find the position of the searchString", () => { | ||
expect(nthIndexOf('test test test', 'test', 2)).toEqual(5); | ||
}); | ||
it("should return undefined when the searchString doesn't exist", () => { | ||
expect(nthIndexOf('bla bla bla', 'test', 2)).toBe(undefined); | ||
}); | ||
}); |
/** | ||
* Check if the string starts with another string | ||
* Checks if `str` starts with the `searchString` | ||
*/ | ||
@@ -9,6 +9,43 @@ export function startsWith(str, searchString) { | ||
/** | ||
* Capitalize the first letter of a string | ||
* Capitalizes the first letter of `str` | ||
*/ | ||
export function capitalize(str) { | ||
return str.charAt(0).toUpperCase() + str.slice(1); | ||
} | ||
/** | ||
* Finds the index of the nth occurence of `searchString` | ||
* | ||
* @see: String.prototype.indexOf | ||
* @see: String.prototype.lastIndexOf | ||
*/ | ||
export function nthIndexOf(str, searchString, n, fromIndex) { | ||
for (var i = 0; i < n && fromIndex !== -1; i++) { | ||
if (fromIndex !== undefined) { | ||
fromIndex += 1; | ||
} | ||
fromIndex = str.indexOf(searchString, fromIndex); | ||
} | ||
return (fromIndex === -1 ? undefined : fromIndex); | ||
} | ||
/** | ||
* Remove the first occurence of `searchString` | ||
*/ | ||
export function cutFirst(str, searchString) { | ||
var index = str.indexOf(searchString); | ||
if (index === -1) { | ||
return str; | ||
} | ||
return str.slice(0, index) + str.slice(index + searchString.length); | ||
} | ||
/** | ||
* Remove everything after `fromIndex` | ||
*/ | ||
export function cutFromIndex(str, fromIndex) { | ||
if (fromIndex === -1 || fromIndex === undefined) { | ||
return str; | ||
} | ||
return str.slice(0, fromIndex); | ||
} |
Sorry, the diff of this file is not supported yet
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
No repository
Supply chain riskPackage does not have a linked source code repository. Without this field, a package will have no reference to the location of the source code use to generate the package.
Found 1 instance in 1 package
4072
8
82
46