string.js
3.4 KB
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
/**
* @fileoverview This module has some functions for handling the string.
* @author NHN.
* FE Development Lab <dl_javascript@nhn.com>
*/
'use strict';
var collection = require('./collection');
var object = require('./object');
/**
* Transform the given HTML Entity string into plain string
* @param {String} htmlEntity - HTML Entity type string
* @returns {String} Plain string
* @memberof tui.util
* @example
* //-- #1. Get Module --//
* var util = require('tui-code-snippet'); // node, commonjs
* var util = tui.util; // distribution file
*
* //-- #2. Use property --//
* var htmlEntityString = "A 'quote' is <b>bold</b>"
* var result = util.decodeHTMLEntity(htmlEntityString); //"A 'quote' is <b>bold</b>"
*/
function decodeHTMLEntity(htmlEntity) {
var entities = {
'"': '"',
'&': '&',
'<': '<',
'>': '>',
''': '\'',
' ': ' '
};
return htmlEntity.replace(/&|<|>|"|'| /g, function(m0) {
return entities[m0] ? entities[m0] : m0;
});
}
/**
* Transform the given string into HTML Entity string
* @param {String} html - String for encoding
* @returns {String} HTML Entity
* @memberof tui.util
* @example
* //-- #1. Get Module --//
* var util = require('tui-code-snippet'); // node, commonjs
* var util = tui.util; // distribution file
*
* //-- #2. Use property --//
* var htmlEntityString = "<script> alert('test');</script><a href='test'>";
* var result = util.encodeHTMLEntity(htmlEntityString);
* //"<script> alert('test');</script><a href='test'>"
*/
function encodeHTMLEntity(html) {
var entities = {
'"': 'quot',
'&': 'amp',
'<': 'lt',
'>': 'gt',
'\'': '#39'
};
return html.replace(/[<>&"']/g, function(m0) {
return entities[m0] ? '&' + entities[m0] + ';' : m0;
});
}
/**
* Return whether the string capable to transform into plain string is in the given string or not.
* @param {String} string - test string
* @memberof tui.util
* @returns {boolean}
*/
function hasEncodableString(string) {
return (/[<>&"']/).test(string);
}
/**
* Return duplicate charters
* @param {string} operandStr1 The operand string
* @param {string} operandStr2 The operand string
* @private
* @memberof tui.util
* @returns {string}
* @example
* //-- #1. Get Module --//
* var util = require('tui-code-snippet'); // node, commonjs
* var util = tui.util; // distribution file
*
* //-- #2. Use property --//
* util.getDuplicatedChar('fe dev', 'nhn entertainment'); // 'e'
* util.getDuplicatedChar('fdsa', 'asdf'); // 'asdf'
*/
function getDuplicatedChar(operandStr1, operandStr2) {
var i = 0;
var len = operandStr1.length;
var pool = {};
var dupl, key;
for (; i < len; i += 1) {
key = operandStr1.charAt(i);
pool[key] = 1;
}
for (i = 0, len = operandStr2.length; i < len; i += 1) {
key = operandStr2.charAt(i);
if (pool[key]) {
pool[key] += 1;
}
}
pool = collection.filter(pool, function(item) {
return item > 1;
});
pool = object.keys(pool).sort();
dupl = pool.join('');
return dupl;
}
module.exports = {
decodeHTMLEntity: decodeHTMLEntity,
encodeHTMLEntity: encodeHTMLEntity,
hasEncodableString: hasEncodableString,
getDuplicatedChar: getDuplicatedChar
};