Skip to content
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

Urls #36

Open
wants to merge 7 commits into
base: master
Choose a base branch
from
Open

Urls #36

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
6 changes: 1 addition & 5 deletions Makefile
Original file line number Diff line number Diff line change
@@ -1,9 +1,5 @@
EXPRESSO = support/expresso/bin/expresso -I lib

TESTS = tests/*.test.js

test:
@$(EXPRESSO) $(TESTS) $(TEST_FLAGS)
mocha

test-cov:
@$(MAKE) TEST_FLAGS=--cov test
Expand Down
1 change: 0 additions & 1 deletion index.js

This file was deleted.

80 changes: 80 additions & 0 deletions lib/email.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
'use strict';

Object.defineProperty(exports, "__esModule", {
value: true
});

var _createClass = function () { function defineProperties(target, props) { for (var i = 0; i < props.length; i++) { var descriptor = props[i]; descriptor.enumerable = descriptor.enumerable || false; descriptor.configurable = true; if ("value" in descriptor) descriptor.writable = true; Object.defineProperty(target, descriptor.key, descriptor); } } return function (Constructor, protoProps, staticProps) { if (protoProps) defineProperties(Constructor.prototype, protoProps); if (staticProps) defineProperties(Constructor, staticProps); return Constructor; }; }();

exports.default = function (Schema, Types) {
var Email = function (_Schema$Types$String) {
_inherits(Email, _Schema$Types$String);

function Email(path, options) {
_classCallCheck(this, Email);

var _this = _possibleConstructorReturn(this, Object.getPrototypeOf(Email).call(this, path, options));

_this.validate(_this.validateEmail, 'Email is invalid');
return _this;
}

_createClass(Email, [{
key: 'cast',
value: function cast(val) {
return val.toLowerCase();
}
}, {
key: 'validateEmail',
value: function validateEmail(val) {
if (!EMAIL_REG.test(val)) {
return false;
}
var maxs = {};
var max = 0;
var acc = val.split('@')[0];

for (var i = acc.length - 1; i >= 0; i--) {
if (acc[i] === acc[i - 1]) {
if (!maxs[acc[i]]) {
maxs[acc[i]] = 1;
}
if (!/[a-z|0-9]/gim.test(acc[i])) {
var z = 0;
if (acc.length > 0 && acc[i].length > 0) {
z = acc.split(acc[i]).length - 1;
}
maxs[acc[i]] += z;
}
if (/[a-z|0-9]/gim.test(acc[i])) {
maxs[acc[i]]++;
}
if (maxs[acc[i + 1]] >= 3) {
maxs[acc[i]]++;
}
}
}

for (var _z in maxs) {
max = maxs[_z] > max ? maxs[_z] : max;
}

return max <= 4;
}
}]);

return Email;
}(Schema.Types.String);

Email.prototype.__proto__ = Schema.Types.String.prototype;
Schema.Types.Email = Email;
Types.Email = String;
};

function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } }

function _possibleConstructorReturn(self, call) { if (!self) { throw new ReferenceError("this hasn't been initialised - super() hasn't been called"); } return call && (typeof call === "object" || typeof call === "function") ? call : self; }

function _inherits(subClass, superClass) { if (typeof superClass !== "function" && superClass !== null) { throw new TypeError("Super expression must either be null or a function, not " + typeof superClass); } subClass.prototype = Object.create(superClass && superClass.prototype, { constructor: { value: subClass, enumerable: false, writable: true, configurable: true } }); if (superClass) Object.setPrototypeOf ? Object.setPrototypeOf(subClass, superClass) : subClass.__proto__ = superClass; }

var EMAIL_REG = /^[a-z|0-9]{1}[a-z|0-9|.|_|-]{1,32}@[a-z|0-9|-]{1,15}(.(ac|ad|ae(ro)?|af|ag|ai|al|am|an|ao|aq|ar|as(ia)?|at|au|aw|ax|az|ba|bb|bd|be|bf|bg|bh|bi(z)?|bj|bm|bn|bo|br|bs|bt|bv|bw|by|bz|ca(t)?|cc|cd|cf|cg|ch|ci|ck|cl|cm|cn|co(m|op)?|cr|cs|cu|cv|cx|cy|cz|dd|de|dj|dk|dm|do|dz|ec|edu|ee|eg|eh|er|es|et|eu(s)?|fi|fj|fk|fm|fo|fr|ga(l)?|gb|gd|ge|gf|gg|gh|gi|gl|gm|gn|gp|gq|gr|gs|gt|gu|gw|gy|hk|hm|hn|hr|ht|hu|id|ie|il|im|in(fo|t)?|io|iq|ir|is|it|je|jm|jo(bs)?|jp|ke|kg|kh|ki|km|kn|kp|kr|kw|ky|kz|la|lb|lc|li|lk|lr|ls|lt|lu|lv|ly|ma|mc|md|me|mg|mh|mk|ml|mm|mn|mo(bi)?|mp|mq|mr|ms|mt|mu(seum)?|mv|mw|mx|my|mz|na(me)?|nc|ne(t)?|nf|ng|ni|nl|no|np|nr|nu|nz|om|org|pa|pe|pf|pg|ph|pk|pl|pm|pn|post|pr(o)?|ps|pt|pw|py|qa|re|ro|rs|ru|rw|sa|sb|sc|sd|se|sg|sh|si|sj|sk|sl|sm|sn|so|sr|ss|st|su|sv|sx|sy|sz|tc|td|tel|tf|tg|th|tj|tk|tl|tm|tn|to|tp|tr(avel)?|tt|tv|tw|tz|ua|ug|uk|us|uy|uz|va|vc|ve|vg|vi|vn|vu|wf|ws|xxx|ye|yt|yu|za|zm|zw)){1,2}$/i;
42 changes: 27 additions & 15 deletions lib/index.js
Original file line number Diff line number Diff line change
@@ -1,17 +1,29 @@
exports.loadTypes = function () {
var mongoose = arguments[0],
types = Array.prototype.slice.call(arguments, 1);
if (types.length) {
types.forEach( function (type) {
require("./types/" + type).loadType(mongoose);
});
} else {
var files = require("fs").readdirSync(__dirname + "/types");
files.forEach( function (filename) {
var base = filename.slice(0, filename.length-3);
require("./types/" + base).loadType(mongoose);
});
}
'use strict';

Object.defineProperty(exports, "__esModule", {
value: true
});

exports.default = function (go) {
var mongo = go || _mongoose2.default;
Url(mongo.Schema, mongo.Types);
Email(mongo.Schema, mongo.Types);

return mongo;
};

exports.useTimestamps = require("./plugins/useTimestamps").useTimestamps;
var _mongoose = require('mongoose');

var _mongoose2 = _interopRequireDefault(_mongoose);

var _email = require('./email');

var Email = _interopRequireWildcard(_email);

var _url = require('./url');

var Url = _interopRequireWildcard(_url);

function _interopRequireWildcard(obj) { if (obj && obj.__esModule) { return obj; } else { var newObj = {}; if (obj != null) { for (var key in obj) { if (Object.prototype.hasOwnProperty.call(obj, key)) newObj[key] = obj[key]; } } newObj.default = obj; return newObj; } }

function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
38 changes: 0 additions & 38 deletions lib/plugins/useTimestamps.js

This file was deleted.

19 changes: 0 additions & 19 deletions lib/types/email.js

This file was deleted.

60 changes: 0 additions & 60 deletions lib/types/url.js

This file was deleted.

56 changes: 56 additions & 0 deletions lib/url.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
'use strict';

Object.defineProperty(exports, "__esModule", {
value: true
});

var _createClass = function () { function defineProperties(target, props) { for (var i = 0; i < props.length; i++) { var descriptor = props[i]; descriptor.enumerable = descriptor.enumerable || false; descriptor.configurable = true; if ("value" in descriptor) descriptor.writable = true; Object.defineProperty(target, descriptor.key, descriptor); } } return function (Constructor, protoProps, staticProps) { if (protoProps) defineProperties(Constructor.prototype, protoProps); if (staticProps) defineProperties(Constructor, staticProps); return Constructor; }; }();

exports.default = function (Schema, Types) {
var Url = function (_Schema$Types$String) {
_inherits(Url, _Schema$Types$String);

function Url(path, options) {
_classCallCheck(this, Url);

var _this = _possibleConstructorReturn(this, Object.getPrototypeOf(Url).call(this, path, options));

_this.validate(_this.validateUrl, 'url is invalid');
return _this;
}

_createClass(Url, [{
key: 'cast',
value: function cast(val) {
var obj = (0, _url.parse)(val, true, true);

delete obj.search;
delete obj.path;
delete obj.href;

return (0, _url.format)(obj);
}
}, {
key: 'validateUrl',
value: function validateUrl(val) {
return URL_REG.test(val);
}
}]);

return Url;
}(Schema.Types.String);

Url.prototype.__proto__ = Schema.Types.String.prototype;
Schema.Types.Url = Url;
Types.Url = String;
};

var _url = require('url');

function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } }

function _possibleConstructorReturn(self, call) { if (!self) { throw new ReferenceError("this hasn't been initialised - super() hasn't been called"); } return call && (typeof call === "object" || typeof call === "function") ? call : self; }

function _inherits(subClass, superClass) { if (typeof superClass !== "function" && superClass !== null) { throw new TypeError("Super expression must either be null or a function, not " + typeof superClass); } subClass.prototype = Object.create(superClass && superClass.prototype, { constructor: { value: subClass, enumerable: false, writable: true, configurable: true } }); if (superClass) Object.setPrototypeOf ? Object.setPrototypeOf(subClass, superClass) : subClass.__proto__ = superClass; }

var URL_REG = /(ftp|http|https):\/\/(\w+:{0,1}\w*@)?(\S+)(:[0-9]+)?(\/|\/([\w#!:.?+=&%@!\-\/]))?/;
37 changes: 26 additions & 11 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,11 +1,26 @@
{ "name": "mongoose-types"
, "description": "More types for mongoose"
, "version": "1.0.3"
, "author": "Brian Noguchi"
, "dependencies": { "mongoose": ">= 1.0.16"}
, "keywords": [ "mongoose", "mongo", "mongodb", "types" ]
, "scripts": { "test": "make test" }
, "engines": { "node": ">= 0.1.101" }
, "main": "./index"
, "licenses": [ { "type": "The MIT License", "url": "http://www.opensource.org/licenses/mit-license.php" } ]
}
{
"name": "mongoose-for-types",
"description": "More types for mongoose",
"version": "2.0.6",
"author": "Brian Noguchi",
"dependencies": {
"mongoose": ">= 1.0.16"
},
"keywords": ["mongoose", "mongo", "mongodb", "types"],
"scripts": {
"test": "make test"
},
"engines": {
"node": ">= 0.1.101"
},
"main": "./lib/",
"licenses": [{
"type": "The MIT License",
"url": "http://www.opensource.org/licenses/mit-license.php"
}],
"babel": {
"presets": [
"es2015"
]
}
}
Loading