Skip to content
This repository has been archived by the owner on Aug 4, 2021. It is now read-only.

Commit

Permalink
v0.0.1
Browse files Browse the repository at this point in the history
Initial commit
  • Loading branch information
Sergej Müller committed Jun 17, 2016
0 parents commit 226e0cb
Show file tree
Hide file tree
Showing 10 changed files with 312 additions and 0 deletions.
14 changes: 14 additions & 0 deletions .codeclimate.yml
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
4 changes: 4 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
node_modules
.npm
logs
*.log
8 changes: 8 additions & 0 deletions .travis.yml
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"
3 changes: 3 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
### v0.0.1 (2016-06-16)

* Initial commit

21 changes: 21 additions & 0 deletions LICENSE.md
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.
56 changes: 56 additions & 0 deletions README.md
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/
```
59 changes: 59 additions & 0 deletions examples/ltrim.js
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/
44 changes: 44 additions & 0 deletions index.js
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;

};
31 changes: 31 additions & 0 deletions package.json
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"
}
}
72 changes: 72 additions & 0 deletions test/test.js
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('');

}

0 comments on commit 226e0cb

Please sign in to comment.