This repository has been archived by the owner on Aug 4, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Initial commit
- Loading branch information
Sergej Müller
committed
Jun 17, 2016
0 parents
commit 226e0cb
Showing
10 changed files
with
312 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
engines: | ||
eslint: | ||
enabled: true | ||
duplication: | ||
enabled: true | ||
exclude_paths: | ||
- test/ | ||
- examples/ | ||
config: | ||
languages: | ||
- javascript | ||
ratings: | ||
paths: | ||
- index.js |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
node_modules | ||
.npm | ||
logs | ||
*.log |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
language: node_js | ||
node_js: | ||
- "6" | ||
- "5" | ||
- "4" | ||
- "0.12" | ||
- "0.11" | ||
- "0.10" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
### v0.0.1 (2016-06-16) | ||
|
||
* Initial commit |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
The MIT License (MIT) | ||
|
||
Copyright (c) 2014 A Medium Corporation | ||
|
||
Permission is hereby granted, free of charge, to any person obtaining a copy | ||
of this software and associated documentation files (the "Software"), to deal | ||
in the Software without restriction, including without limitation the rights | ||
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell | ||
copies of the Software, and to permit persons to whom the Software is | ||
furnished to do so, subject to the following conditions: | ||
|
||
The above copyright notice and this permission notice shall be included in | ||
all copies or substantial portions of the Software. | ||
|
||
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | ||
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, | ||
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE | ||
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER | ||
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, | ||
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN | ||
THE SOFTWARE. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,56 @@ | ||
ltrim | ||
============ | ||
|
||
`ltrim` [Node.js module](https://www.npmjs.com/package/ltrim) returns a string with whitespace (or other characters) stripped from the beginning of a string. Without dependencies and library bloat. | ||
|
||
|
||
[![Dependency Status](https://david-dm.org/sergejmueller/ltrim.svg)](https://david-dm.org/sergejmueller/ltrim) | ||
[![Code Climate](https://codeclimate.com/github/sergejmueller/ltrim/badges/gpa.svg)](https://codeclimate.com/github/sergejmueller/ltrim) | ||
[![Build Status](https://travis-ci.org/sergejmueller/ltrim.svg?branch=master)](https://travis-ci.org/sergejmueller/ltrim) | ||
|
||
|
||
Install | ||
----- | ||
|
||
``` | ||
npm install ltrim | ||
``` | ||
|
||
|
||
Usage | ||
----- | ||
|
||
Without the second parameter, `ltrim()` will strip whitespaces (spaces, tabs and new lines). | ||
|
||
```javascript | ||
var ltrim = require('ltrim'); | ||
|
||
|
||
/** | ||
* Strip whitespace from the beginning of a string | ||
*/ | ||
|
||
ltrim( ' Hello ' ) + ' World' // Hello World | ||
|
||
|
||
/** | ||
* Strip multiple special chars from the beginning of a string | ||
* e.g. space & dot | ||
*/ | ||
|
||
ltrim( '... Hello World ...', ' .' ); // Hello World ... | ||
|
||
|
||
/** | ||
* Strip multiple chars from the beginning of a string | ||
*/ | ||
|
||
ltrim( 'Hello World', 'Hdle' ); // o World | ||
|
||
|
||
/** | ||
* Strip url protocol from the beginning of a string | ||
*/ | ||
|
||
ltrim( 'https://goo.gl/', '/:htps' ); // goo.gl/ | ||
``` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,59 @@ | ||
"use strict"; | ||
|
||
|
||
var ltrim = require('..'); | ||
|
||
|
||
/** | ||
* Strip whitespace from the beginning of a string | ||
*/ | ||
|
||
console.log( | ||
ltrim( | ||
' Hello ' | ||
) + ' World' | ||
); | ||
|
||
// Hello World | ||
|
||
|
||
/** | ||
* Strip multiple special chars (e.g. space & dot) from the beginning of a string | ||
*/ | ||
|
||
console.log( | ||
ltrim( | ||
'... Hello World ...', | ||
' .' | ||
) | ||
); | ||
|
||
// Hello World ... | ||
|
||
|
||
/** | ||
* Strip multiple chars from the beginning of a string | ||
*/ | ||
|
||
console.log( | ||
ltrim( | ||
'Hello World', | ||
'Hdle' | ||
) | ||
); | ||
|
||
// o World | ||
|
||
|
||
/** | ||
* Strip url protocol from the beginning of a string | ||
*/ | ||
|
||
console.log( | ||
ltrim( | ||
'https://goo.gl/', | ||
'/:htps' | ||
) | ||
); | ||
|
||
// goo.gl/ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
"use strict"; | ||
|
||
|
||
/** | ||
* Strip whitespace - or other characters - from the beginning of a string | ||
* | ||
* @param {String} str Input string | ||
* @param {String} chars Character(s) to strip [optional] | ||
* @return {String} str Modified string | ||
*/ | ||
|
||
module.exports = function ( str, chars ) { | ||
|
||
// Convert to string | ||
str = str.toString(); | ||
|
||
// Empty string? | ||
if ( ! str ) { | ||
return ''; | ||
} | ||
|
||
// Remove whitespace | ||
if ( ! chars ) { | ||
return str.replace( /^\s+/, '' ); | ||
} | ||
|
||
// Convert to string | ||
chars = chars.toString(); | ||
|
||
// Set vars | ||
var i = 0, | ||
letters = str.split(''), | ||
count = letters.length; | ||
|
||
// Loop letters | ||
for ( i; i < count; i ++ ) { | ||
if ( chars.indexOf( letters[i] ) === -1 ) { | ||
return str.substring( i ); | ||
} | ||
} | ||
|
||
return str; | ||
|
||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
{ | ||
"name": "ltrim", | ||
"version": "0.0.1", | ||
"description": "Strip whitespace - or other characters - from the beginning of a string", | ||
"main": "index.js", | ||
"scripts": { | ||
"test": "mocha" | ||
}, | ||
"keywords": [ | ||
"trim", | ||
"ltrim", | ||
"strip", | ||
"string" | ||
], | ||
"author": "Sergej Müller", | ||
"homepage": "https://sergejmueller.github.io", | ||
"repository": { | ||
"type": "git", | ||
"url": "https://github.com/sergejmueller/ltrim.git" | ||
}, | ||
"engines": { | ||
"node": "0.10 || 0.11 || 0.12 || 4 || 5 || 6" | ||
}, | ||
"license": "MIT", | ||
"devDependencies": { | ||
"chai": "^3.5.0", | ||
"mocha": "^2.5.1", | ||
"array-range": "^1.0.1", | ||
"shuffle-array": "^1.0.0" | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,72 @@ | ||
"use strict"; | ||
|
||
|
||
var ltrim = require('..'), | ||
range = require('array-range'), | ||
shuffle = require('shuffle-array'), | ||
expect = require('chai').expect; | ||
|
||
|
||
describe( 'Strip characters from the beginning of a string', function() { | ||
|
||
|
||
// Strip url protocol from the beginning | ||
it( 'Strip url protocol', function() { | ||
|
||
expect( | ||
ltrim( 'https://goo.gl/', '/:htps' ) | ||
).to.equal( | ||
'goo.gl/' | ||
); | ||
|
||
} ); | ||
|
||
|
||
// Strip whitespace from the beginning (default) | ||
it( 'Strip whitespace', function() { | ||
|
||
expect( | ||
ltrim( ' Hello World ' ) | ||
).to.equal( | ||
'Hello World ' | ||
); | ||
|
||
} ); | ||
|
||
|
||
// Strip random special chars from the beginning | ||
it( 'Strip random special chars', function() { | ||
|
||
expect( | ||
ltrim( | ||
shuffledSpecialChars() + 'Hello World', | ||
shuffledSpecialChars() // shuffle again | ||
) | ||
).to.equal( | ||
'Hello World' | ||
); | ||
|
||
} ); | ||
|
||
|
||
} ); | ||
|
||
|
||
/** | ||
* Get shuffled special chars from a custom code range | ||
* | ||
* @return {String} Shuffled special characters | ||
*/ | ||
|
||
var shuffledSpecialChars = function() { | ||
|
||
var chars = []; | ||
|
||
range( 32, 65 ).concat( [91, 92, 93, 94, 95, 96, 123, 124, 125, 126] ) | ||
.forEach( function( _ ) { | ||
chars.push( String.fromCharCode( _ ) ); | ||
} ); | ||
|
||
return shuffle( chars ).join(''); | ||
|
||
} |