diff --git a/.codeclimate.yml b/.codeclimate.yml new file mode 100644 index 0000000..77809e2 --- /dev/null +++ b/.codeclimate.yml @@ -0,0 +1,14 @@ +engines: + eslint: + enabled: true + duplication: + enabled: true + exclude_paths: + - test/ + - examples/ + config: + languages: + - javascript +ratings: + paths: + - index.js \ No newline at end of file diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..8db0ae0 --- /dev/null +++ b/.gitignore @@ -0,0 +1,4 @@ +node_modules +.npm +logs +*.log \ No newline at end of file diff --git a/.travis.yml b/.travis.yml new file mode 100644 index 0000000..65cf1d9 --- /dev/null +++ b/.travis.yml @@ -0,0 +1,8 @@ +language: node_js +node_js: + - "6" + - "5" + - "4" + - "0.12" + - "0.11" + - "0.10" \ No newline at end of file diff --git a/CHANGELOG.md b/CHANGELOG.md new file mode 100644 index 0000000..6ced219 --- /dev/null +++ b/CHANGELOG.md @@ -0,0 +1,3 @@ +### v0.0.1 (2016-06-16) + +* Initial commit
 \ No newline at end of file diff --git a/LICENSE.md b/LICENSE.md new file mode 100644 index 0000000..4accad9 --- /dev/null +++ b/LICENSE.md @@ -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. diff --git a/README.md b/README.md new file mode 100644 index 0000000..626062b --- /dev/null +++ b/README.md @@ -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/ +``` diff --git a/examples/ltrim.js b/examples/ltrim.js new file mode 100644 index 0000000..8331bbb --- /dev/null +++ b/examples/ltrim.js @@ -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/ diff --git a/index.js b/index.js new file mode 100644 index 0000000..31393c0 --- /dev/null +++ b/index.js @@ -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; + +}; diff --git a/package.json b/package.json new file mode 100644 index 0000000..9a46393 --- /dev/null +++ b/package.json @@ -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" + } +} diff --git a/test/test.js b/test/test.js new file mode 100644 index 0000000..ff8ed48 --- /dev/null +++ b/test/test.js @@ -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(''); + +}