forked from funsocietyirc/MrNodeBot
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathhelpers.js
169 lines (147 loc) · 6.34 KB
/
helpers.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
/**
* @module Helpers
* @author Dave Richer
*/
const moment = require('moment');
const accounting = require('accounting-js');
const c = require('irc-colors');
const _ = require('lodash');
/**
* Format a number (using separators)
* @param input
*/
const formatNumber = module.exports.formatNumber = input => accounting.formatNumber(input, {
precision: 0,
});
/**
* Check to see if a function is async or not
* @param func
* @return {boolean}
*/
const isAsync = module.exports.isAsync = func => func.toString().toLowerCase().startsWith('async');
/**
* AccessString - Get A String representation of a Access Level
* @param {String} str Number value of the access string
* @returns {String} A String representation of the access value
*/
const AccessString = module.exports.AccessString = (str) => {
switch (str) {
case 0:
return 'Guest';
case 1:
return 'Identified';
case 2:
return 'Administrator';
case 3:
return 'Owner';
case 4:
return 'ChannelOp';
case 5:
return 'ChannelVoice';
case 6:
return 'ChannelOpIdentified';
case 7:
return 'ChannelVoiceIdentified';
default:
return 'Unknown';
}
};
/**
* Search a iterable for a value and return the first key
* @param {Map} map The Iterable
* @param {Object} value The value to search for
* @returns {Boolean} the first match found
*/
const MapSearch = /**
*/
module.exports.MapSearch = (map, value) => {
for (const [t, v] of map) {
if (v === value) return t;
}
return false;
};
/** Start Time in moment format */
const StartTime = module.exports.StartTime = moment();
/* Color stuffs */
/**
* ColorHelpArgs - Colorize brackets
* @param {String} text Un-formatted String
* @returns {String} Un-formatted String
*/
const ColorHelpArgs = module.exports.ColorHelpArgs = text => text.replace(/([\[\],])/g, c.red.bold('$1'));
/**
* RedSlashes - Colorize Slashes
* @param {String} text Un-formatted String
* @returns {String} Formatted String
*/
const RedSlashes = module.exports.RedSlashes = text => text.replace(/(\/)/g, c.red.bold('$1'));
/**
* TitleLine - Format a Title Line
* @param {String} text Un-formatted String
* @returns {String} Formatted String
*/
const TitleLine = module.exports.TitleLine = text => c.white.bold.bgblack(text);
/**
* Plural - Basic Pluralization
* @param {String} text Text to apply Transform
* @param {Number} number Amount
* @returns {String} Formatted String
*/
const Plural = module.exports.Plural = (text, number) => (number > 1 || number === 0 ? `${text}s` : text);
/**
* StripNewLine - Strip New Lines
* @param {String} text Un-formatted Text
* @returns {String} Formatted Text
*/
const StripNewLine = module.exports.StripNewLine = text => text.replace(/\r?\n|\r/g, ' ');
/**
* Replace all occurrences of needle in haystack with replacement
* @param {String} haystack the text to search in
* @param {String} needle what we are looking for
* @param {String} replacement what to replace the needle with
* @returns {String} haystack with needles replaced by replacement
*/
const ReplaceAll = module.exports.ReplaceAll = (haystack, needle, replacement) => {
if (!_.isString(haystack) || !_.isString(needle) || !_.isString(replacement)) return haystack;
return haystack.replace(new RegExp(needle.replace(/([\/\,\!\\\^\$\{\}\[\]\(\)\.\*\+\?\|\<\>\-\&])/g, '\\$&'), 'g'), replacement.replace(/\$/g, '$$$$'));
};
/**
* Rot13 - 13 Letter shift encoder/decoder
* @param {String} text Un-formatted Text
* @returns {String} Formatted Text
*/
const Rot13 = module.exports.Rot13 = text => text.replace(/[a-zA-Z]/g, c => String.fromCharCode((c <= 'Z' ? 90 : 122) >= (c = c.charCodeAt(0) + 13) ? c : c - 26));
/** Valid Hosts Expression * */
const ValidHostExpression = module.exports.ValidHostExpression = /^(([0-9]|[1-9][0-9]|1[0-9]{2}|2[0-4][0-9]|25[0-5])\.){3}([0-9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f]{1,4}){1,7})|((:[0-9A-Fa-f]{1,4}){0,5}:((25[0-5]|2[0-4]\d|1\d\d|[1-9]?\d)(\.(25[0-5]|2[0-4]\d|1\d\d|[1-9]?\d)){3}))|:)))(%.+)?\s*$/;
/** Match Non printable characters */
const RemoveNonPrintChars = module.exports.RemoveNonPrintChars = /[\u0002\u001F\u0016\u0003\u000F]/g;
/** Match Fake Space characters */
const FakeSpaceChars = module.exports.FakeSpaceChars = /[\u0009\u000A\u000B\u000C\u000D\u0085\u00A0\u1680\u2000\u2001\u2002\u2003\u2004\u2005\u2006\u2007\u2008\u2009\u200A\u2029\u202F\u205F\u3000\u180E\u200B\u200C\u200D\u2060\uFEFF]/g;
/**
* Youtube Expression
**/
const YoutubeExpression = module.exports.YoutubeExpression = /^.*(youtu\.be\/|v\/|u\/\w\/|embed\/|watch\?v=|\&v=)([^#\&\?]*).*/;
/**
* Get Key From Object
* @param obj
* @param index
* @returns {string}
*/
const getKeyFromObj = module.exports.getKeyFromObj = (obj, index) => Object.getOwnPropertyNames(obj).slice(index)[0];
/**
* Get Value From Object
* @param obj
* @param index
* @returns {*}
*/
const getValueFromObj = module.exports.getValueFromObj = (obj, index) => obj[Object.keys(obj)[Object.keys(obj).length + index]];
/**
* Chunk Objects
* @type {function(*=, *=): *}
*/
const chunkObj = module.exports.chunkObj = (input, size) => _.chain(input).toPairs().chunk(size).map(_.fromPairs).value();
/**
* Hash Pattern Replacement Regex
* @type {RegExp}
*/
const hashPattern = module.exports.hashPattern = new RegExp('%23', 'g');