Skip to content

Additional parsing methods #213

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 15 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
node_modules/
coverage/
.DS_Store
69 changes: 67 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -154,6 +154,16 @@ See [test file][testfile] for more details.

I use the same nomenclature as Objective-C regarding methods. **+** means `static` or `class` method. **-** means `non-static` or `instance` method.

### - append(data) ###

Appends the given string in data to the end of the string.

Example:

```javascript
S('Hello').append(' World') //"Hello World"
```

### - constructor(nativeJsString) ###

This creates a new `string.js` object. The parameter can be anything. The `toString()` method will be called on any objects. Some native objects are used in some functions such as `toCSV()`.
Expand Down Expand Up @@ -297,6 +307,28 @@ S('3 &lt; 4').decodeHTMLEntities().s; //'3 < 4'
```


### - delLeftMost(find) ###

Returns the remaining string after removing the first occurrence of `find` and everything to the left of `find` when scanning from **left to right**. If `find` is not found, the unmodified string is returned.

Example:

```javascript
S('/Parent/child/folders').delLeftMost('/child').s; //'/folders'
```


### - delRightMost(find) ###

Returns the remaining string after removing the first occurrence of `find` and everything to the right of `find` when scanning from **right to left**. If `find` is not found, the unmodified string is returned.

Example:

```javascript
S('/Parent/child/folders').delRightMost('/child').s; //'/Parent'
```


### - endsWith(ss) ###

Returns true if the string ends with `ss`.
Expand Down Expand Up @@ -330,7 +362,6 @@ S.extendPrototype();
```



### - ensureLeft(prefix)

Ensures string starts with `prefix`.
Expand All @@ -343,6 +374,28 @@ S('/subdir').ensureLeft('/').s; //'/subdir'
```


### - getRightMost(find) ###

Finds the first occurrence of `find` and returns everything to the right of `find` (not including `find` itself) when scanning from **right to left**. If `find` is not found, the unmodified string is returned.

Example:

```javascript
S('/Parent/child/folders').getRightMost('/child').s; //'/folders'
```


### - getLeftMost(find) ###

Finds the first occurrence of `find` and returns everything to the left of `find` (not including `find` itself) when scanning from **left to right**. If `find` is not found, the unmodified string is returned.

Example:

```javascript
S('/Parent/child/folders').getLeftMost('/child').s; //'/Parent'
```


### - ensureRight(suffix)

Ensures string ends with `suffix`.
Expand All @@ -354,6 +407,7 @@ S('dir').ensureRight('/').s; //'dir/'
S('dir/').ensureRight('/').s; //'dir/'
```


### - humanize() ###

Transforms the input into a human friendly form.
Expand Down Expand Up @@ -603,6 +657,16 @@ S('"a","b\\"","d","c"').parseCSV() //['a', 'b"', 'd', 'c'])
S('"a\na","b","c"\n"a", """b\nb", "a"').parseCSV(',', '"', '"', '\n')) // [ [ 'a\na', 'b', 'c' ], [ 'a', '"b\nb', 'a' ] ]
```

### - prepend(data) ###

Prepends the given string in data to the beginning of the string.

Example:

```javascript
S('World').prepend('Hello ') //"Hello World"
```

### - repeat(n) ###

Returns a string repeated `n` times.
Expand Down Expand Up @@ -705,7 +769,7 @@ S('On Rock N Roll and other Stuff').splitLeft(' ', 5, -2); // ['and', 'other Stu

### - splitRight(sep, [maxSplit = -1, [limit]]) ###

Returns an array of strings, split from the left at `sep`. Performs at most `maxSplit` splits, and slices the result into an array with at most `limit` elements.
Returns an array of strings, split from the right at `sep`. Performs at most `maxSplit` splits, and slices the result into an array with at most `limit` elements.

Example:

Expand Down Expand Up @@ -1137,6 +1201,7 @@ If you contribute to this library, just modify `string.js`, `string.test.js`, an
- [*] [Alison Rowland](https://github.com/arowla)
- [*] [Pascal Bihler](https://github.com/pbihler)
- [*] [Daniel Diekmeier](https://github.com/danieldiekmeier)
- [*] [Stephen J. Carnam](https://github.com/Steveorevo)



Expand Down
108 changes: 102 additions & 6 deletions dist/string.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,11 @@
!function(e){if("object"==typeof exports)module.exports=e();else if("function"==typeof define&&define.amd)define(e);else{var f;"undefined"!=typeof window?f=window:"undefined"!=typeof global?f=global:"undefined"!=typeof self&&(f=self),f.S=e()}}(function(){var define,module,exports;return (function e(t,n,r){function s(o,u){if(!n[o]){if(!t[o]){var a=typeof require=="function"&&require;if(!u&&a)return a(o,!0);if(i)return i(o,!0);throw new Error("Cannot find module '"+o+"'")}var f=n[o]={exports:{}};t[o][0].call(f.exports,function(e){var n=t[o][1][e];return s(n?n:e)},f,f.exports,e,t,n,r)}return n[o].exports}var i=typeof require=="function"&&require;for(var o=0;o<r.length;o++)s(r[o]);return s})({1:[function(_dereq_,module,exports){
function append(self, ss) {
var s = self.s + ss;
return new self.constructor(s);
};
module.exports = append;

},{}],2:[function(_dereq_,module,exports){
function count(self, substr) {
var count = 0
var pos = self.indexOf(substr)
Expand All @@ -12,7 +19,73 @@ function count(self, substr) {
}

module.exports = count
},{}],2:[function(_dereq_,module,exports){
},{}],3:[function(_dereq_,module,exports){
function delLeftMost(self, find) {
var s = self.s;
for (var i = 0; i < s.length; i = i + 1) {
var f = s.indexOf(find, i);
if (f != -1) {
return new self.constructor(s.substr(f + find.length, s.length));
break;
}
}
return new self.constructor(s);
};

module.exports = delLeftMost;

},{}],4:[function(_dereq_,module,exports){
function delRightMost(self, find) {
var s = self.s;
for (var i = s.length; i >= 0; i = i - 1) {
var f = s.indexOf(find, i);
if (f != -1) {
return new self.constructor(s.substr(0, f));
break;
}
}
return new self.constructor(s);
};

module.exports = delRightMost;

},{}],5:[function(_dereq_,module,exports){
function getLeftMost(self, find) {
var s = self.s;
for (var i = 0; i < s.length; i = i + 1) {
var f = s.indexOf(find, i);
if (f != -1) {
return new self.constructor(s.substr(0, f));
break;
}
}
return new self.constructor(s);
};

module.exports = getLeftMost;

},{}],6:[function(_dereq_,module,exports){
function getRightMost(self, find) {
var s = self.s;
for (var i = s.length; i >= 0; i = i - 1) {
var f = s.indexOf(find, i);
if (f != -1) {
return new self.constructor(s.substr(f + find.length, s.length));
}
}
return new self.constructor(s);
};

module.exports = getRightMost;

},{}],7:[function(_dereq_,module,exports){
function prepend(self, ss) {
var s = ss + self.s;
return new self.constructor(s);
};
module.exports = prepend;

},{}],8:[function(_dereq_,module,exports){
function splitLeft(self, sep, maxSplit, limit) {

if (typeof maxSplit === 'undefined') {
Expand Down Expand Up @@ -41,7 +114,7 @@ function splitLeft(self, sep, maxSplit, limit) {

module.exports = splitLeft;

},{}],3:[function(_dereq_,module,exports){
},{}],9:[function(_dereq_,module,exports){
function splitRight(self, sep, maxSplit, limit) {

if (typeof maxSplit === 'undefined') {
Expand Down Expand Up @@ -74,7 +147,7 @@ function splitRight(self, sep, maxSplit, limit) {

module.exports = splitRight;

},{}],4:[function(_dereq_,module,exports){
},{}],10:[function(_dereq_,module,exports){
/*
string.js - Copyright (C) 2012-2014, JP Richardson <[email protected]>
*/
Expand Down Expand Up @@ -138,6 +211,9 @@ string.js - Copyright (C) 2012-2014, JP Richardson <[email protected]>

var __nsp = String.prototype;
var __sp = S.prototype = {
append: function(ss) {
return _dereq_('./_append')(this, ss)
},

between: function(left, right) {
var s = this.s;
Expand Down Expand Up @@ -206,7 +282,7 @@ string.js - Copyright (C) 2012-2014, JP Richardson <[email protected]>
var s = this.trim().s.replace(/[_\s]+/g, '-').replace(/([A-Z])/g, '-$1').replace(/-+/g, '-').toLowerCase();
return new this.constructor(s);
},

equalsIgnoreCase: function(prefix) {
var s = this.s;
return s.toLowerCase() == prefix.toLowerCase()
Expand Down Expand Up @@ -243,6 +319,14 @@ string.js - Copyright (C) 2012-2014, JP Richardson <[email protected]>
return new this.constructor(s);
},

delLeftMost: function(find) {
return _dereq_('./_delLeftMost')(this, find)
},

delRightMost: function(find) {
return _dereq_('./_delRightMost')(this, find)
},

endsWith: function() {
var suffixes = Array.prototype.slice.call(arguments, 0);
for (var i = 0; i < suffixes.length; ++i) {
Expand Down Expand Up @@ -274,6 +358,14 @@ string.js - Copyright (C) 2012-2014, JP Richardson <[email protected]>
}
},

getLeftMost: function(find) {
return _dereq_('./_getLeftMost')(this, find)
},

getRightMost: function(find) {
return _dereq_('./_getRightMost')(this, find)
},

humanize: function() { //modified from underscore.string
if (this.s === null || this.s === undefined)
return new this.constructor('')
Expand Down Expand Up @@ -423,6 +515,10 @@ string.js - Copyright (C) 2012-2014, JP Richardson <[email protected]>
return fields;
},

prepend: function(ss) {
return _dereq_('./_prepend')(this, ss)
},

replaceAll: function(ss, r) {
//var s = this.s.replace(new RegExp(ss, 'g'), r);
var s = this.s.split(ss).join(r)
Expand Down Expand Up @@ -1184,6 +1280,6 @@ string.js - Copyright (C) 2012-2014, JP Richardson <[email protected]>

}).call(this);

},{"./_count":1,"./_splitLeft":2,"./_splitRight":3}]},{},[4])
(4)
},{"./_append":1,"./_count":2,"./_delLeftMost":3,"./_delRightMost":4,"./_getLeftMost":5,"./_getRightMost":6,"./_prepend":7,"./_splitLeft":8,"./_splitRight":9}]},{},[10])
(10)
});
2 changes: 1 addition & 1 deletion dist/string.min.js

Large diffs are not rendered by default.

5 changes: 5 additions & 0 deletions lib/_append.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
function append(self, ss) {
var s = self.s + ss;
return new self.constructor(s);
};
module.exports = append;
13 changes: 13 additions & 0 deletions lib/_delLeftMost.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
function delLeftMost(self, find) {
var s = self.s;
for (var i = 0; i < s.length; i = i + 1) {
var f = s.indexOf(find, i);
if (f != -1) {
return new self.constructor(s.substr(f + find.length, s.length));
break;
}
}
return new self.constructor(s);
};

module.exports = delLeftMost;
13 changes: 13 additions & 0 deletions lib/_delRightMost.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
function delRightMost(self, find) {
var s = self.s;
for (var i = s.length; i >= 0; i = i - 1) {
var f = s.indexOf(find, i);
if (f != -1) {
return new self.constructor(s.substr(0, f));
break;
}
}
return new self.constructor(s);
};

module.exports = delRightMost;
13 changes: 13 additions & 0 deletions lib/_getLeftMost.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
function getLeftMost(self, find) {
var s = self.s;
for (var i = 0; i < s.length; i = i + 1) {
var f = s.indexOf(find, i);
if (f != -1) {
return new self.constructor(s.substr(0, f));
break;
}
}
return new self.constructor(s);
};

module.exports = getLeftMost;
12 changes: 12 additions & 0 deletions lib/_getRightMost.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
function getRightMost(self, find) {
var s = self.s;
for (var i = s.length; i >= 0; i = i - 1) {
var f = s.indexOf(find, i);
if (f != -1) {
return new self.constructor(s.substr(f + find.length, s.length));
}
}
return new self.constructor(s);
};

module.exports = getRightMost;
5 changes: 5 additions & 0 deletions lib/_prepend.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
function prepend(self, ss) {
var s = ss + self.s;
return new self.constructor(s);
};
module.exports = prepend;
Loading