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 16, 2016
0 parents commit 747075a
Show file tree
Hide file tree
Showing 10 changed files with 311 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 @@
rtrim
============

`rtrim` Node.js module returns a string with whitespace (or other characters) stripped from the end of a string. Without dependencies and library bloat.


[![Dependency Status](https://david-dm.org/sergejmueller/rtrim.svg)](https://david-dm.org/sergejmueller/rtrim)
[![Code Climate](https://codeclimate.com/github/sergejmueller/rtrim/badges/gpa.svg)](https://codeclimate.com/github/sergejmueller/rtrim)
[![Build Status](https://travis-ci.org/sergejmueller/rtrim.svg?branch=master)](https://travis-ci.org/sergejmueller/rtrim)


Install
-----

```
npm install rtrim
```


Usage
-----

Without the second parameter, `rtrim()` will strip whitespaces (spaces, tabs and new lines).

```javascript
var rtrim = require('rtrim');


/**
* Strip whitespace from the end of a string
*/

rtrim(' Hello ' ) + ' World' // Hello World


/**
* Strip multiple special chars from the end of a string
* e.g. space & dot
*/

rtrim( 'Hello World ...', ' .' ); // Hello World


/**
* Strip multiple chars from the end of a string
*/

rtrim( 'Hello World', 'Hdle' ); // Hello Wor


/**
* Strip trailing slash from the end of a string
*/

rtrim( 'https://goo.gl/', '/' ); // https://goo.gl
```
59 changes: 59 additions & 0 deletions examples/rtrim.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
"use strict";


var rtrim = require('..');


/**
* Strip whitespace from the end of a string
*/

console.log(
rtrim(
' Hello '
) + ' World'
);

// Hello World


/**
* Strip multiple special chars (e.g. space & dot) from the end of a string
*/

console.log(
rtrim(
'Hello World ...',
' .'
)
);

// Hello World


/**
* Strip multiple chars from the end of a string
*/

console.log(
rtrim(
'Hello World',
'Hdle'
)
);

// Hello Wor


/**
* Strip trailing slash from the end of a string
*/

console.log(
rtrim(
'https://goo.gl/',
'/'
)
);

// https://goo.gl
43 changes: 43 additions & 0 deletions index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
"use strict";


/**
* Strip whitespace - or other characters - from the end 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 arg is empty
if ( typeof chars === 'undefined' ) {
return str.replace( /\s+$/, '' );
}

// Convert to string
chars = chars.toString();

// Set vars
var letters = str.split('');
var i = letters.length - 1;

// Loop letters
for ( i; i >= 0; i --) {
if ( chars.indexOf( letters[i] ) === -1 ) {
return str.substring(0, i + 1);
}
}

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": "rtrim",
"version": "0.0.1",
"description": "Strip whitespace - or other characters - from the end of a string",
"main": "index.js",
"scripts": {
"test": "mocha"
},
"keywords": [
"trim",
"rtrim",
"strip",
"string"
],
"author": "Sergej Müller",
"homepage": "https://sergejmueller.github.io",
"repository": {
"type": "git",
"url": "https://github.com/sergejmueller/rtrim.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 rtrim = require('..'),
range = require('array-range'),
shuffle = require('shuffle-array'),
expect = require('chai').expect;


describe( 'Strip characters from the end of a string', function() {


// Strip trailing slash from the end
it( 'Strip trailing slash', function() {

expect(
rtrim( 'https://goo.gl/', '/' )
).to.equal(
'https://goo.gl'
);

} );


// Strip whitespace from the end (default)
it( 'Strip whitespace', function() {

expect(
rtrim( 'Hello World ' )
).to.equal(
'Hello World'
);

} );


// Strip random special chars from the end
it( 'Strip random special chars', function() {

expect(
rtrim(
'Hello World' + shuffledSpecialChars(),
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 747075a

Please sign in to comment.