forked from homerquan/mongoose-html
-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
42 lines (35 loc) · 998 Bytes
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
'use strict';
var mongoose = require('mongoose'),
sanitizeHtml = require('sanitize-html'),
util = require('util');
var setting = {};
module.exports.loadType = function(mongoose) {
mongoose.Types.Html = mongoose.SchemaTypes.Html = Html;
return Html;
};
function Html(path, options) {
setting = options.setting;
mongoose.SchemaTypes.String.call(this, path, options);
}
/*!
* inherits
*/
util.inherits(Html, mongoose.SchemaTypes.String);
Html.prototype.cast = function(val) {
if (isType('String', val)) {
var dirty = val.toString();
var clean = sanitizeHtml(dirty, setting);
return clean;
} else {
return new Error('Should pass in a string');
}
};
/**
* isType(type, obj)
* Supported types: 'Function', 'String', 'Number', 'Date', 'RegExp',
* 'Arguments'
* source: https://github.com/jashkenas/underscore/blob/1.5.2/underscore.js#L996
*/
function isType(type, obj) {
return Object.prototype.toString.call(obj) == '[object ' + type + ']';
}