Skip to content

Commit f3d9735

Browse files
author
Christoph Hermann
committed
move functions to commonjs modules
closes #355
1 parent eb7dfee commit f3d9735

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

73 files changed

+854
-709
lines changed

.gitignore

+1
Original file line numberDiff line numberDiff line change
@@ -3,3 +3,4 @@
33
.project
44
.tmp_*
55
node_modules
6+
underscore.string.compiled.js

.travis.yml

-5
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,3 @@
11
language: node_js
22
node_js:
33
- "0.11"
4-
5-
before_script:
6-
- "export DISPLAY=:99.0"
7-
- "sh -e /etc/init.d/xvfb start"
8-
- sleep 2

README.markdown

+11-1
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ _(" epeli ").chain().trim().capitalize().value()
2222

2323
## Download ##
2424

25-
* [Development version](https://raw.github.com/epeli/underscore.string/master/lib/underscore.string.js) *Uncompressed with Comments 18kb*
25+
* [Development version](https://raw.github.com/epeli/underscore.string/master/underscore.string.js) *Uncompressed with Comments 18kb*
2626
* [Production version](https://github.com/epeli/underscore.string/raw/master/dist/underscore.string.min.js) *Minified 7kb*
2727

2828

@@ -38,6 +38,16 @@ _(" epeli ").chain().trim().capitalize().value()
3838
var _s = require('underscore.string');
3939
```
4040

41+
**Import specific modules (browserify/node)**:
42+
43+
```javascript
44+
var capitalize = require('underscore.string/capitalize');
45+
var trim = require('underscore.string/trim');
46+
47+
capitalize(trim(" epeli "));
48+
=> "Epeli"
49+
```
50+
4151
**Integrate with Underscore.js**:
4252

4353
```javascript

bower.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@
1717
"underscore",
1818
"string"
1919
],
20-
"main": "./lib/underscore.string.js",
20+
"main": "./underscore.string.js",
2121
"repository": {
2222
"type": "git",
2323
"url": "https://github.com/epeli/underscore.string.git"

camelize.js

+7
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
var trim = require('./trim');
2+
3+
module.exports = function camelize(str) {
4+
return trim(str).replace(/[-_\s]+(.)?/g, function(match, c) {
5+
return c ? c.toUpperCase() : "";
6+
});
7+
};

capitalize.js

+6
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
var makeString = require('./helper/makeString');
2+
3+
module.exports = function capitalize(str) {
4+
str = makeString(str);
5+
return str.charAt(0).toUpperCase() + str.slice(1);
6+
};

chars.js

+5
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
var makeString = require('./helper/makeString');
2+
3+
module.exports = function chars(str) {
4+
return makeString(str).split('');
5+
};

chop.js

+6
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
module.exports = function chop(str, step) {
2+
if (str == null) return [];
3+
str = String(str);
4+
step = ~~step;
5+
return step > 0 ? str.match(new RegExp('.{1,' + step + '}', 'g')) : [str];
6+
};

classify.js

+6
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
var capitalize = require('./capitalize');
2+
var camelize = require('./camelize');
3+
4+
module.exports = function classify(str) {
5+
return capitalize(camelize(String(str).replace(/[\W_]/g, ' ')).replace(/\s/g, ''));
6+
};

clean.js

+5
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
var trim = require('./trim');
2+
3+
module.exports = function clean(str) {
4+
return trim(str).replace(/\s+/g, ' ');
5+
};

component.json

+2-2
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,6 @@
66
"keywords": ["underscore", "string"],
77
"dependencies": {},
88
"development": {},
9-
"main": "lib/underscore.string.js",
10-
"scripts": ["lib/underscore.string.js"]
9+
"main": "underscore.string.js",
10+
"scripts": ["underscore.string.js"]
1111
}

count.js

+19
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
var makeString = require('./helper/makeString');
2+
3+
module.exports = function(str, substr) {
4+
str = makeString(str);
5+
substr = makeString(substr);
6+
7+
var count = 0,
8+
pos = 0,
9+
length = substr.length;
10+
11+
while (true) {
12+
pos = str.indexOf(substr, pos);
13+
if (pos === -1) break;
14+
count++;
15+
pos += length;
16+
}
17+
18+
return count;
19+
};

dasherize.js

+5
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
var trim = require('./trim');
2+
3+
module.exports = function dasherize(str) {
4+
return trim(str).replace(/([A-Z])/g, '-$1').replace(/[-_\s]+/g, '-').toLowerCase();
5+
};

decapitalize.js

+6
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
var makeString = require('./helper/makeString');
2+
3+
module.exports = function decapitalize(str) {
4+
str = makeString(str);
5+
return str.charAt(0).toLowerCase() + str.slice(1);
6+
};

endsWith.js

+13
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
var makeString = require('./helper/makeString');
2+
var toPositive = require('./helper/toPositive');
3+
4+
module.exports = function endsWith(str, ends, position) {
5+
str = makeString(str);
6+
ends = '' + ends;
7+
if (typeof position == 'undefined') {
8+
position = str.length - ends.length;
9+
} else {
10+
position = Math.min(toPositive(position), str.length) - ends.length;
11+
}
12+
return position >= 0 && str.indexOf(ends, position) === position;
13+
};

escapeHTML.js

+12
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
var makeString = require('./helper/makeString');
2+
var escapeChars = require('./helper/escapeChars');
3+
var reversedEscapeChars = {};
4+
5+
for(var key in escapeChars) reversedEscapeChars[escapeChars[key]] = key;
6+
reversedEscapeChars["'"] = '#39';
7+
8+
module.exports = function escapeHTML(str) {
9+
return makeString(str).replace(/[&<>"']/g, function(m) {
10+
return '&' + reversedEscapeChars[m] + ';';
11+
});
12+
};

exports.js

+10
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
module.exports = function() {
2+
var result = {};
3+
4+
for (var prop in this) {
5+
if (!this.hasOwnProperty(prop) || prop.match(/^(?:include|contains|reverse)$/)) continue;
6+
result[prop] = this[prop];
7+
}
8+
9+
return result;
10+
};

gulpfile.js

+44-32
Original file line numberDiff line numberDiff line change
@@ -1,50 +1,62 @@
11
var gulp = require('gulp-param')(require('gulp'), process.argv),
2-
qunit = require("gulp-qunit"),
3-
uglify = require('gulp-uglify'),
4-
clean = require('gulp-clean'),
5-
bump = require('gulp-bump'),
6-
replace = require('gulp-replace'),
7-
rename = require('gulp-rename'),
8-
SRC = 'lib/underscore.string.js',
9-
DEST = 'dist',
10-
MIN_FILE = 'underscore.string.min.js',
11-
TEST_SUITES = ['test/test.html', 'test/test_underscore/index.html'],
12-
VERSION_FILES = ['./package.json', './component.json'];
2+
qunit = require("gulp-qunit"),
3+
uglify = require('gulp-uglify'),
4+
clean = require('gulp-clean'),
5+
bump = require('gulp-bump'),
6+
replace = require('gulp-replace'),
7+
rename = require('gulp-rename'),
8+
browserify = require('gulp-browserify'),
9+
SRC = 'underscore.string.js',
10+
SRC_COMPILED = 'underscore.string.compiled.js',
11+
DEST = 'dist',
12+
MIN_FILE = 'underscore.string.min.js',
13+
TEST_SUITES = ['test/test.html', 'test/test_standalone.html', 'test/test_underscore/index.html'],
14+
VERSION_FILES = ['./package.json', './component.json'];
1315

14-
gulp.task('test', function() {
15-
return gulp.src(TEST_SUITES)
16-
.pipe(qunit());
16+
gulp.task('test', ['browserify'], function() {
17+
return gulp.src(TEST_SUITES)
18+
.pipe(qunit());
19+
});
20+
21+
gulp.task('browserify', function() {
22+
return gulp.src(SRC)
23+
.pipe(browserify({
24+
detectGlobals: true,
25+
standalone: 'underscore.string'
26+
}))
27+
.pipe(rename('underscore.string.compiled.js'))
28+
.pipe(gulp.dest('./'));
1729
});
1830

1931
gulp.task('clean', function() {
20-
return gulp.src(DEST)
21-
.pipe(clean());
32+
return gulp.src(DEST)
33+
.pipe(clean());
2234
});
2335

2436
gulp.task('bump-in-js', function(semver) {
25-
gulp.src(SRC)
26-
.pipe(replace(/(version:?\s\')([\d\.]*)\'/gi, '$1' + semver + "'"))
27-
.pipe(gulp.dest('./lib'));
37+
return gulp.src(SRC)
38+
.pipe(replace(/(version:?\s\')([\d\.]*)\'/gi, '$1' + semver + "'"))
39+
.pipe(gulp.dest('./'));
2840
});
2941

3042
// usage: gulp bump -s <% Version %>
3143
// usage: gulp bump --semver <% Version %>
3244
gulp.task('bump', ['bump-in-js'], function(semver) {
33-
if (typeof semver !== 'string' || semver.length <= 0) {
34-
console.error('pass a new version `gulp bump --semver 2.4.1`');
35-
process.exit(1);
36-
}
45+
if (typeof semver !== 'string' || semver.length <= 0) {
46+
console.error('pass a new version `gulp bump --semver 2.4.1`');
47+
process.exit(1);
48+
}
3749

38-
return gulp.src(VERSION_FILES)
39-
.pipe(bump({
40-
version: semver
41-
}))
42-
.pipe(gulp.dest('./'));
50+
return gulp.src(VERSION_FILES)
51+
.pipe(bump({
52+
version: semver
53+
}))
54+
.pipe(gulp.dest('./'));
4355
});
4456

4557
gulp.task('build', ['test', 'clean'], function() {
46-
return gulp.src(SRC)
47-
.pipe(uglify())
48-
.pipe(rename(MIN_FILE))
49-
.pipe(gulp.dest(DEST));
58+
return gulp.src(SRC_COMPILED)
59+
.pipe(uglify())
60+
.pipe(rename(MIN_FILE))
61+
.pipe(gulp.dest(DEST));
5062
});

helper/defaultToWhiteSpace.js

+10
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
var escapeRegExp = require('./escapeRegExp');
2+
3+
module.exports = function defaultToWhiteSpace(characters) {
4+
if (characters == null)
5+
return '\\s';
6+
else if (characters.source)
7+
return characters.source;
8+
else
9+
return '[' + escapeRegExp(characters) + ']';
10+
};

helper/escapeChars.js

+9
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
var escapeChars = {
2+
lt: '<',
3+
gt: '>',
4+
quot: '"',
5+
amp: '&',
6+
apos: "'"
7+
};
8+
9+
module.exports = escapeChars;

helper/escapeRegExp.js

+5
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
var makeString = require('./makeString');
2+
3+
module.exports = function escapeRegExp(str) {
4+
return makeString(str).replace(/([.*+?^=!:${}()|[\]\/\\])/g, '\\$1');
5+
};

helper/makeString.js

+7
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
/**
2+
* Ensure some object is a coerced to a string
3+
**/
4+
module.exports = function makeString(object) {
5+
if (object == null) return '';
6+
return '' + object;
7+
};

helper/strRepeat.js

+9
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
module.exports = function strRepeat(str, qty){
2+
if (qty < 1) return '';
3+
var result = '';
4+
while (qty > 0) {
5+
if (qty & 1) result += str;
6+
qty >>= 1, str += str;
7+
}
8+
return result;
9+
};

helper/toPositive.js

+3
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
module.exports = function toPositive(number) {
2+
return number < 0 ? 0 : (+number || 0);
3+
};

humanize.js

+6
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
var capitalize = require('./capitalize');
2+
var underscored = require('./underscored');
3+
4+
module.exports = function humanize(str) {
5+
return capitalize(underscored(str).replace(/_id$/, '').replace(/_/g, ' '));
6+
};

include.js

+6
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
var makeString = require('./helper/makeString');
2+
3+
module.exports = function include(str, needle) {
4+
if (needle === '') return true;
5+
return makeString(str).indexOf(needle) !== -1;
6+
};

insert.js

+5
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
var splice = require('./splice');
2+
3+
module.exports = function insert(str, i, substr) {
4+
return splice(str, i, 0, substr);
5+
};

isBlank.js

+5
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
var makeString = require('./helper/makeString');
2+
3+
module.exports = function isBlank(str) {
4+
return (/^\s*$/).test(makeString(str));
5+
};

join.js

+9
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
var makeString = require('./helper/makeString');
2+
var slice = [].slice;
3+
4+
module.exports = function join() {
5+
var args = slice.call(arguments),
6+
separator = args.shift();
7+
8+
return args.join(makeString(separator));
9+
};

levenshtein.js

+25
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
var makeString = require('./helper/makeString');
2+
3+
module.exports = function levenshtein(str1, str2) {
4+
str1 = makeString(str1);
5+
str2 = makeString(str2);
6+
7+
var current = [],
8+
prev, value;
9+
10+
for (var i = 0; i <= str2.length; i++)
11+
for (var j = 0; j <= str1.length; j++) {
12+
if (i && j)
13+
if (str1.charAt(j - 1) === str2.charAt(i - 1))
14+
value = prev;
15+
else
16+
value = Math.min(current[j], current[j - 1], prev) + 1;
17+
else
18+
value = i + j;
19+
20+
prev = current[j];
21+
current[j] = value;
22+
}
23+
24+
return current.pop();
25+
};

0 commit comments

Comments
 (0)