Skip to content

Commit

Permalink
Merge pull request #480 from marcodejongh/remove_stringjs
Browse files Browse the repository at this point in the history
Remove stringjs dependency
  • Loading branch information
aldeed committed Nov 12, 2015
2 parents 3dade1d + 42262cd commit b7f927a
Show file tree
Hide file tree
Showing 9 changed files with 79 additions and 972 deletions.
1 change: 0 additions & 1 deletion .npm/package/.gitignore

This file was deleted.

7 changes: 0 additions & 7 deletions .npm/package/README

This file was deleted.

7 changes: 0 additions & 7 deletions .npm/package/npm-shrinkwrap.json

This file was deleted.

12 changes: 12 additions & 0 deletions humanize-tests.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
Tinytest.add("Humanize - Should humanize a string", function (test) {
test.equal(humanize('super_snake_case'), 'Super snake case')
test.equal(humanize('capitalizedCamelCase'), 'Capitalized camel case')
test.equal(humanize('hyphen-case'), 'Hyphen case')
test.equal(humanize('no-extensions-here.md'), 'No extensions here')
test.equal(humanize('lower cased phrase'), 'Lower cased phrase')
test.equal(humanize(' so many spaces '), 'So many spaces')
test.equal(humanize(123), '123')
test.equal(humanize(''), '')
test.equal(humanize(null), '')
test.equal(humanize(undefined), '')
});
11 changes: 5 additions & 6 deletions package.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,6 @@ Package.describe({
git: "https://github.com/aldeed/meteor-simple-schema.git"
});

Npm.depends({
"string": "1.6.0"
});

Package.on_use(function(api) {

if (api.versionsFrom) {
Expand All @@ -20,8 +16,9 @@ Package.on_use(function(api) {
api.use(['deps', 'underscore', 'check', 'random']);
}

api.add_files('string.js', 'client');
api.add_files([
'string-polyfills.js',
'string-humanize.js',
'mongo-object.js',
'simple-schema-utility.js',
'simple-schema.js',
Expand All @@ -30,6 +27,8 @@ Package.on_use(function(api) {
'simple-schema-context.js'
]);
api.export(['SimpleSchema', 'MongoObject'], ['client', 'server']);
api.export('humanize', {testonly:true});

});

Package.on_test(function(api) {
Expand All @@ -44,5 +43,5 @@ Package.on_test(function(api) {
api.use(["simple-schema", "tinytest", "test-helpers", "underscore", "check"]);
}

api.add_files(["simple-schema-tests.js", "mongo-object-tests.js"], ['client', 'server']);
api.add_files(["simple-schema-tests.js", "mongo-object-tests.js", "humanize-tests.js"], ['client', 'server']);
});
18 changes: 5 additions & 13 deletions simple-schema.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,6 @@
/* global SimpleSchemaValidationContext */
/* global MongoObject */
/* global Utility */
/* global S:true */

if (Meteor.isServer) {
S = Npm.require("string");
}
if (Meteor.isClient) {
S = window.S;
}

var schemaDefinition = {
type: Match.Any,
Expand Down Expand Up @@ -49,7 +41,7 @@ var typeconvert = function(value, type) {
return value;
}
if (type === Number) {
if (typeof value === "string" && !S(value).isEmpty()) {
if (typeof value === "string" && !_.isEmpty(value)) {
//try to convert numeric strings to numbers
var numberVal = Number(value);
if (!isNaN(numberVal)) {
Expand Down Expand Up @@ -258,7 +250,7 @@ var getObjectKeys = function(schema, schemaKeyList) {

loopArray = [];
_.each(schemaKeyList, function(fieldName2) {
if (S(fieldName2).startsWith(keyPrefix)) {
if (fieldName2.startsWith(keyPrefix)) {
remainingText = fieldName2.substring(keyPrefix.length);
if (remainingText.indexOf(".") === -1) {
loopArray.push(remainingText);
Expand All @@ -283,7 +275,7 @@ var inflectedLabel = function(fieldName) {
if (label === "_id") {
return "ID";
}
return S(label).humanize().s;
return humanize(label);
};

/**
Expand Down Expand Up @@ -754,7 +746,7 @@ SimpleSchema.prototype.clean = function(doc, options) {
var newVal = typeconvert(val, def.type);
// trim strings
if (options.trimStrings && typeof newVal === "string") {
newVal = S(newVal).trim().s;
newVal = newVal.trim();
}
if (newVal !== void 0 && newVal !== val) {
// remove empty strings
Expand All @@ -778,7 +770,7 @@ SimpleSchema.prototype.clean = function(doc, options) {
if (!wasAutoConverted) {
// trim strings
if (options.trimStrings && typeof val === "string" && (!def || (def && def.trim !== false))) {
this.updateValue(S(val).trim().s);
this.updateValue(val.trim());
}
// remove empty strings
if (options.removeEmptyStrings && (!this.operator || this.operator === "$set") && typeof val === "string" && !val.length) {
Expand Down
45 changes: 45 additions & 0 deletions string-humanize.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
/*
Code source:
https://github.com/jxson/string-humanize
https://github.com/jxson/string-capitalize
*/

function capitalize(string){
string = string || '';
string = string.trim();

if (string[0]) {
string = string[0].toUpperCase() + string.substr(1).toLowerCase();
}

return string;
}

humanize = function humanize(string){
string = string || '';
string = string.toString(); // might be a number
string = string.trim();
string = string.replace(extname(string), '');
string = underscore(string);
string = string.replace(/[\W_]+/g, ' ');

return capitalize(string);
}


function underscore(string){
string = string || '';
string = string.toString(); // might be a number
string = string.trim();
string = string.replace(/([a-z\d])([A-Z]+)/g, '$1_$2');
string = string.replace(/[-\s]+/g, '_').toLowerCase();

return string;
}

function extname(string){
var index = string.lastIndexOf('.');
var ext = string.substring(index, string.length);

return (index === -1) ? '' : ext;
}
12 changes: 12 additions & 0 deletions string-polyfills.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
if (!String.prototype.startsWith) {
String.prototype.startsWith = function(searchString, position) {
position = position || 0;
return this.indexOf(searchString, position) === position;
};
}

if (!String.prototype.trim) {
String.prototype.trim = function () {
return this.replace(/^[\s\uFEFF\xA0]+|[\s\uFEFF\xA0]+$/g, '');
};
}
Loading

0 comments on commit b7f927a

Please sign in to comment.