-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgrawlix.js
169 lines (158 loc) · 5.7 KB
/
grawlix.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
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
'use strict';
const _ = require('underscore');
const util = require('./util');
const Style = require('./styles').Style;
const GrawlixPlugin = require('./plugin').GrawlixPlugin;
const GrawlixPluginError = require('./plugin').GrawlixPluginError;
/**
* Grawlix default options
* @type {Object}
*/
var defaultOptions = {
style: Style.ASCII,
randomize: true,
plugins: [],
filters: [],
styles: [],
allowed: []
};
/**
* Cached settings object; no need to repeat that work if the options haven't
* changed.
* @type {GrawlixSettings}
*/
var defaultSettings = null;
/**
* Replaces all curse words in the given content string with cartoon-like
* grawlixes.
* @param {String} str Content string
* @param {Object} options Options object. Optional.
* @param {Object} options.style Style of grawlix to use for replacements.
* Can be either a string, with the name of
* the style to use; or an object with a
* required `name` property. See readme for
* more details and available options.
* Defaults to the `ascii` style.
* @param {Boolean} options.randomize Whether or not to replace curses with
* randomized or fixed grawlixes. Default is
* true.
* @param {Array} options.allowed Array of strings, representing
* whitelisted words that would otherwise be
* replaced. Optional.
* @param {Array} options.filters Array of custom filter objects. These can
* either reconfigure one of the existing
* default filter, or represent an entirely
* new filter. See readme for details.
* Optional.
* @param {Array} options.plugins Array of either plugin factory functions
* or GrawlixPlugin objects. See docs for
* details. Optional.
* @return {String} Processed string
*/
var grawlix = function(str, options) {
// get settings
var settings;
if (_.isUndefined(options) && defaultSettings !== null) {
settings = defaultSettings;
} else if (!_.isUndefined(options)) {
settings = util.parseOptions(options, defaultOptions);
} else {
settings = util.parseOptions(defaultOptions);
defaultSettings = settings;
}
// return processed string
return util.replaceMatches(str, settings);
};
/**
* Get default options
* @return {Object} Default options object
*/
grawlix.getDefaults = function() {
return defaultOptions;
};
/**
* Set default options
* @param {Object} options Defaults to set
*/
grawlix.setDefaults = function(options) {
defaultOptions = _.extend({}, defaultOptions, options);
defaultSettings = null;
};
/**
* Returns whether or not the given string contains any profanity matches
* @param {String} str Content string
* @param {Array} filters Array of filter objects. Optional.
* @param {Array} allowed Array of words to allow. Optional.
* @return {Boolean} True if a filter matches, false otherwise.
*/
grawlix.isObscene = function(str, filters, allowed) {
// get settings
var settings;
if (_.isUndefined(filters) && _.isUndefined(allowed) &&
defaultSettings !== null) {
settings = defaultSettings;
} else if (_.isArray(filters) || _.isArray(allowed)) {
settings = util.parseOptions({
filters: (!_.isUndefined(filters) && filters !== null) ? filters : [],
allowed: (!_.isUndefined(allowed) && allowed !== null) ? allowed : []
}, defaultOptions);
} else {
settings = util.parseOptions(defaultOptions);
defaultSettings = settings;
}
// return if there's a filter match
return util.isMatch(str, settings);
};
/**
* Adds a plugin to the default options.
* @param {*} plugin Either a GrawlixPlugin instance, a factory
* function that returns a GrawlixPlugin
* instance, or a module name to load via
* require.
* @param {Object} pluginOptions Plugin-specific options. Optional.
* @return {grawlix} Return self for chaining
*/
grawlix.loadPlugin = function(plugin, pluginOptions) {
var obj = {};
if (plugin instanceof GrawlixPlugin || _.isFunction(plugin)) {
obj.plugin = plugin;
} else if (_.isString(plugin)) {
// presume it's a module name to load
obj.module = plugin;
} else {
throw new GrawlixPluginError({
message: 'invalid plugin',
plugin: plugin,
trace: new Error()
});
}
obj.options = !_.isUndefined(pluginOptions) ? pluginOptions : {};
defaultOptions.plugins.push(obj);
// return self for chaining
return grawlix;
};
/**
* Returns whether or not given plugin has been added to the default options.
* @param {*} plugin Plugin name, module id, GrawlixPlugin instance, or
* factory function.
* @return {Boolean}
*/
grawlix.hasPlugin = function(plugin) {
return util.hasPlugin(plugin, defaultOptions);
};
/**
* Enum and class exports
*/
grawlix.Style = Style;
grawlix.GrawlixPlugin = GrawlixPlugin;
grawlix.FilterTemplate = require('./filters').FilterTemplate;
grawlix.error = {
GrawlixFilterError: require('./filters').GrawlixFilterError,
GrawlixPluginError: GrawlixPluginError,
GrawlixStyleError: require('./styles').GrawlixStyleError
};
/**
* Export
* @type {Function}
*/
module.exports = grawlix;