-
Notifications
You must be signed in to change notification settings - Fork 0
/
can-string.js
143 lines (142 loc) · 4.26 KB
/
can-string.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
'use strict';
// ##string.js
// _Miscellaneous string utility functions._
// Several of the methods in this plugin use code adapted from Prototype
// Prototype JavaScript framework, version 1.6.0.1.
// © 2005-2007 Sam Stephenson
var strUndHash = /_|-/,
strColons = /\=\=/,
strWords = /([A-Z]+)([A-Z][a-z])/g,
strLowUp = /([a-z\d])([A-Z])/g,
strDash = /([a-z\d])([A-Z])/g,
strQuote = /"/g,
strSingleQuote = /'/g,
strHyphenMatch = /-+(.)?/g,
strCamelMatch = /[a-z][A-Z]/g,
convertBadValues = function (content) {
// Convert bad values into empty strings
var isInvalid = content === null || content === undefined || isNaN(content) && '' + content === 'NaN';
return '' + (isInvalid ? '' : content);
};
var string = {
/**
* @function can-string.esc esc
* @signature `string.esc(content)`
* @param {String} content a string
* @return {String} the string safely HTML-escaped
*
* ```js
* var string = require("can-string");
*
* string.esc("<div> </div>"); //-> "<div>&nbsp;</div>"
* ```
*/
esc: function (content) {
return convertBadValues(content)
.replace(/&/g, '&')
.replace(/</g, '<')
.replace(/>/g, '>')
.replace(strQuote, '"')
.replace(strSingleQuote, ''');
},
/**
* @function can-string.capitalize capitalize
* @signature `string.capitalize(s)`
* @param {String} s the string to capitalize
* @return {String} the supplied string with the first character uppercased if it is a letter
*
* ```js
* var string = require("can-string");
*
* console.log(string.capitalize("foo")); // -> "Foo"
* console.log(string.capitalize("123")); // -> "123"
* ```
*/
capitalize: function (s) {
// Used to make newId.
return s.charAt(0)
.toUpperCase() + s.slice(1);
},
/**
* @function can-string.camelize camelize
* @signature `string.camelize(s)`
* @param {String} str the string to camelCase
* @return {String} the supplied string with hyphens removed and following letters capitalized.
*
* ```js
* var string = require("can-string");
*
* console.log(string.camelize("foo-bar")); // -> "fooBar"
* console.log(string.camelize("-webkit-flex-flow")); // -> "WebkitFlexFlow"
* ```
*/
camelize: function (str) {
return convertBadValues(str)
.replace(strHyphenMatch, function (match, chr) {
return chr ? chr.toUpperCase() : '';
});
},
/**
* @function can-string.hyphenate hyphenate
* @signature `string.hyphenate(s)`
* @param {String} str a string in camelCase
* @return {String} the supplied string with camelCase converted to hyphen-lowercase digraphs
*
* ```js
* var string = require("can-string");
*
* console.log(string.hyphenate("fooBar")); // -> "foo-bar"
* console.log(string.hyphenate("WebkitFlexFlow")); // -> "Webkit-flex-flow"
* ```
*/
hyphenate: function (str) {
return convertBadValues(str)
.replace(strCamelMatch, function (str) {
return str.charAt(0) + '-' + str.charAt(1)
.toLowerCase();
});
},
/**
* @function can-string.pascalize pascalize
* @signature `string.pascalize(s)`
* @param {String} str the string in hyphen case | camelCase
* @return {String} the supplied string with hyphens | camelCase converted to PascalCase
*
* ```js
* var string = require("can-string");
*
* console.log(string.pascalize("fooBar")); // -> "FooBar"
* console.log(string.pascalize("baz-bar")); // -> "BazBar"
* ```
*/
pascalize: function (str) {
return string.capitalize(string.camelize(str));
},
/**
* @function can-string.underscore underscore
* @signature `string.underscore(s)`
* @param {String} str a string in camelCase
* @return {String} the supplied string with camelCase converted to underscore-lowercase digraphs
*
* ```js
* var string = require("can-string");
*
* console.log(string.underscore("fooBar")); // -> "foo_bar"
* console.log(string.underscore("HTMLElement")); // -> "html_element"
* ```
*/
underscore: function (s) {
return s.replace(strColons, '/')
.replace(strWords, '$1_$2')
.replace(strLowUp, '$1_$2')
.replace(strDash, '_')
.toLowerCase();
},
/**
* @property {RegExp} can-string.strUndHash strUndHash
*
* A regex which matches an underscore or hyphen character
*/
undHash: strUndHash
};
module.exports = string;