@util.js/strings 0.41.6 → 0.41.7
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/Strings.js +115 -0
- package/package.json +5 -1
- package/lib/Strings.js +0 -93
- package/test/StringsTest.js +0 -147
package/dist/Strings.js
ADDED
|
@@ -0,0 +1,115 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
|
|
3
|
+
/**
|
|
4
|
+
* JavaScript utility methods for strings
|
|
5
|
+
* @public
|
|
6
|
+
* @class
|
|
7
|
+
*/
|
|
8
|
+
var _interopRequireDefault = require("@babel/runtime-corejs3/helpers/interopRequireDefault");
|
|
9
|
+
var _indexOf = _interopRequireDefault(require("@babel/runtime-corejs3/core-js-stable/instance/index-of"));
|
|
10
|
+
var _fromCodePoint = _interopRequireDefault(require("@babel/runtime-corejs3/core-js-stable/string/from-code-point"));
|
|
11
|
+
var _raw = _interopRequireDefault(require("@babel/runtime-corejs3/core-js-stable/string/raw"));
|
|
12
|
+
var _classCallCheck2 = _interopRequireDefault(require("@babel/runtime-corejs3/helpers/classCallCheck"));
|
|
13
|
+
var _createClass2 = _interopRequireDefault(require("@babel/runtime-corejs3/helpers/createClass"));
|
|
14
|
+
var Strings = /*#__PURE__*/function () {
|
|
15
|
+
function Strings() {
|
|
16
|
+
(0, _classCallCheck2["default"])(this, Strings);
|
|
17
|
+
}
|
|
18
|
+
return (0, _createClass2["default"])(Strings, [{
|
|
19
|
+
key: "base64ToBase64Url",
|
|
20
|
+
value: function base64ToBase64Url() {
|
|
21
|
+
var _base64url;
|
|
22
|
+
return (_base64url = base64url()).fromBase64.apply(_base64url, arguments);
|
|
23
|
+
}
|
|
24
|
+
}, {
|
|
25
|
+
key: "base64UrlDecode",
|
|
26
|
+
value: function base64UrlDecode() {
|
|
27
|
+
var _base64url2;
|
|
28
|
+
return (_base64url2 = base64url()).decode.apply(_base64url2, arguments);
|
|
29
|
+
}
|
|
30
|
+
}, {
|
|
31
|
+
key: "base64UrlEncode",
|
|
32
|
+
value: function base64UrlEncode() {
|
|
33
|
+
var _base64url3;
|
|
34
|
+
return (_base64url3 = base64url()).encode.apply(_base64url3, arguments);
|
|
35
|
+
}
|
|
36
|
+
}, {
|
|
37
|
+
key: "base64UrlToBase64",
|
|
38
|
+
value: function base64UrlToBase64() {
|
|
39
|
+
var _base64url4;
|
|
40
|
+
return (_base64url4 = base64url()).toBase64.apply(_base64url4, arguments);
|
|
41
|
+
}
|
|
42
|
+
}, {
|
|
43
|
+
key: "base64UrlToBuffer",
|
|
44
|
+
value: function base64UrlToBuffer() {
|
|
45
|
+
var _base64url5;
|
|
46
|
+
return (_base64url5 = base64url()).toBuffer.apply(_base64url5, arguments);
|
|
47
|
+
}
|
|
48
|
+
|
|
49
|
+
// https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String/endsWith
|
|
50
|
+
}, {
|
|
51
|
+
key: "endsWith",
|
|
52
|
+
value: function endsWith(str, searchString, position) {
|
|
53
|
+
var subjectString = str.toString();
|
|
54
|
+
if (position === undefined || position > subjectString.length) position = subjectString.length;
|
|
55
|
+
position -= searchString.length;
|
|
56
|
+
var lastIndex = (0, _indexOf["default"])(subjectString).call(subjectString, searchString, position);
|
|
57
|
+
return lastIndex !== -1 && lastIndex === position;
|
|
58
|
+
}
|
|
59
|
+
}, {
|
|
60
|
+
key: "fromCharCode",
|
|
61
|
+
value: function fromCharCode() {
|
|
62
|
+
return String.fromCharCode.apply(String, arguments);
|
|
63
|
+
}
|
|
64
|
+
}, {
|
|
65
|
+
key: "fromCodePoint",
|
|
66
|
+
value: function fromCodePoint() {
|
|
67
|
+
return _fromCodePoint["default"].apply(String, arguments);
|
|
68
|
+
}
|
|
69
|
+
|
|
70
|
+
/**
|
|
71
|
+
* Returns whether the specified object is an instance of string or not.
|
|
72
|
+
*
|
|
73
|
+
* This implementation comes from a [Stack Overflow answer]{@link https://stackoverflow.com/a/9436948}.
|
|
74
|
+
*
|
|
75
|
+
* @param {Object} object - the object to test
|
|
76
|
+
* @returns {boolean} true if the specified object is an instance of string and false otherwise
|
|
77
|
+
* @public
|
|
78
|
+
* @function
|
|
79
|
+
*/
|
|
80
|
+
}, {
|
|
81
|
+
key: "isString",
|
|
82
|
+
value: function isString(object) {
|
|
83
|
+
return typeof object === "string" || object instanceof String;
|
|
84
|
+
}
|
|
85
|
+
}, {
|
|
86
|
+
key: "pad",
|
|
87
|
+
value: function pad(n, width, z) {
|
|
88
|
+
z = z || "0";
|
|
89
|
+
z = z + "";
|
|
90
|
+
if (z.length > 1) throw new Error("The padding character must have a length of 1 but was " + z.length);
|
|
91
|
+
n = n + "";
|
|
92
|
+
return n.length >= width ? n : new Array(width - n.length + 1).join(z) + n;
|
|
93
|
+
}
|
|
94
|
+
}, {
|
|
95
|
+
key: "raw",
|
|
96
|
+
value: function raw() {
|
|
97
|
+
return _raw["default"].apply(String, arguments);
|
|
98
|
+
}
|
|
99
|
+
|
|
100
|
+
// https://css-tricks.com/snippets/javascript/strip-html-tags-in-javascript/
|
|
101
|
+
}, {
|
|
102
|
+
key: "stripTags",
|
|
103
|
+
value: function stripTags(str) {
|
|
104
|
+
return str.replace(/(<([^>]+)>)/gi, "");
|
|
105
|
+
}
|
|
106
|
+
}]);
|
|
107
|
+
}();
|
|
108
|
+
module.exports = Strings;
|
|
109
|
+
var dependencies = {};
|
|
110
|
+
function get(dependency) {
|
|
111
|
+
return dependencies[dependency] || (dependencies[dependency] = require(dependency));
|
|
112
|
+
}
|
|
113
|
+
function base64url() {
|
|
114
|
+
return get("base64url");
|
|
115
|
+
}
|
package/package.json
CHANGED
|
@@ -9,6 +9,10 @@
|
|
|
9
9
|
"work in https://npm.runkit.com."
|
|
10
10
|
],
|
|
11
11
|
"description": "JavaScript utility methods for strings",
|
|
12
|
+
"files": [
|
|
13
|
+
"dist",
|
|
14
|
+
"index.js"
|
|
15
|
+
],
|
|
12
16
|
"keywords": [
|
|
13
17
|
"base64url",
|
|
14
18
|
"endsWith",
|
|
@@ -34,5 +38,5 @@
|
|
|
34
38
|
"type": "git",
|
|
35
39
|
"url": "https://github.com/creemama/utiljs/tree/master/packages/utiljs-strings"
|
|
36
40
|
},
|
|
37
|
-
"version": "0.41.
|
|
41
|
+
"version": "0.41.7"
|
|
38
42
|
}
|
package/lib/Strings.js
DELETED
|
@@ -1,93 +0,0 @@
|
|
|
1
|
-
"use strict";
|
|
2
|
-
|
|
3
|
-
/**
|
|
4
|
-
* JavaScript utility methods for strings
|
|
5
|
-
* @public
|
|
6
|
-
* @class
|
|
7
|
-
*/
|
|
8
|
-
class Strings {
|
|
9
|
-
base64ToBase64Url() {
|
|
10
|
-
return base64url().fromBase64(...arguments);
|
|
11
|
-
}
|
|
12
|
-
|
|
13
|
-
base64UrlDecode() {
|
|
14
|
-
return base64url().decode(...arguments);
|
|
15
|
-
}
|
|
16
|
-
|
|
17
|
-
base64UrlEncode() {
|
|
18
|
-
return base64url().encode(...arguments);
|
|
19
|
-
}
|
|
20
|
-
|
|
21
|
-
base64UrlToBase64() {
|
|
22
|
-
return base64url().toBase64(...arguments);
|
|
23
|
-
}
|
|
24
|
-
|
|
25
|
-
base64UrlToBuffer() {
|
|
26
|
-
return base64url().toBuffer(...arguments);
|
|
27
|
-
}
|
|
28
|
-
|
|
29
|
-
// https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String/endsWith
|
|
30
|
-
endsWith(str, searchString, position) {
|
|
31
|
-
const subjectString = str.toString();
|
|
32
|
-
if (position === undefined || position > subjectString.length)
|
|
33
|
-
position = subjectString.length;
|
|
34
|
-
position -= searchString.length;
|
|
35
|
-
const lastIndex = subjectString.indexOf(searchString, position);
|
|
36
|
-
return lastIndex !== -1 && lastIndex === position;
|
|
37
|
-
}
|
|
38
|
-
|
|
39
|
-
fromCharCode() {
|
|
40
|
-
return String.fromCharCode(...arguments);
|
|
41
|
-
}
|
|
42
|
-
|
|
43
|
-
fromCodePoint() {
|
|
44
|
-
return String.fromCodePoint(...arguments);
|
|
45
|
-
}
|
|
46
|
-
|
|
47
|
-
/**
|
|
48
|
-
* Returns whether the specified object is an instance of string or not.
|
|
49
|
-
*
|
|
50
|
-
* This implementation comes from a [Stack Overflow answer]{@link https://stackoverflow.com/a/9436948}.
|
|
51
|
-
*
|
|
52
|
-
* @param {Object} object - the object to test
|
|
53
|
-
* @returns {boolean} true if the specified object is an instance of string and false otherwise
|
|
54
|
-
* @public
|
|
55
|
-
* @function
|
|
56
|
-
*/
|
|
57
|
-
isString(object) {
|
|
58
|
-
return typeof object === "string" || object instanceof String;
|
|
59
|
-
}
|
|
60
|
-
|
|
61
|
-
pad(n, width, z) {
|
|
62
|
-
z = z || "0";
|
|
63
|
-
z = z + "";
|
|
64
|
-
if (z.length > 1)
|
|
65
|
-
throw new Error(
|
|
66
|
-
"The padding character must have a length of 1 but was " + z.length,
|
|
67
|
-
);
|
|
68
|
-
n = n + "";
|
|
69
|
-
return n.length >= width ? n : new Array(width - n.length + 1).join(z) + n;
|
|
70
|
-
}
|
|
71
|
-
|
|
72
|
-
raw() {
|
|
73
|
-
return String.raw(...arguments);
|
|
74
|
-
}
|
|
75
|
-
|
|
76
|
-
// https://css-tricks.com/snippets/javascript/strip-html-tags-in-javascript/
|
|
77
|
-
stripTags(str) {
|
|
78
|
-
return str.replace(/(<([^>]+)>)/gi, "");
|
|
79
|
-
}
|
|
80
|
-
}
|
|
81
|
-
|
|
82
|
-
module.exports = Strings;
|
|
83
|
-
|
|
84
|
-
const dependencies = {};
|
|
85
|
-
function get(dependency) {
|
|
86
|
-
return (
|
|
87
|
-
dependencies[dependency] || (dependencies[dependency] = require(dependency))
|
|
88
|
-
);
|
|
89
|
-
}
|
|
90
|
-
|
|
91
|
-
function base64url() {
|
|
92
|
-
return get("base64url");
|
|
93
|
-
}
|
package/test/StringsTest.js
DELETED
|
@@ -1,147 +0,0 @@
|
|
|
1
|
-
"use strict";
|
|
2
|
-
|
|
3
|
-
const { expect } = require("chai");
|
|
4
|
-
const strings = require("..");
|
|
5
|
-
|
|
6
|
-
describe("Strings", () => {
|
|
7
|
-
describe("#base64Url...", () => {
|
|
8
|
-
it("should operate normally", () => {
|
|
9
|
-
// From https://www.npmjs.com/package/base64url
|
|
10
|
-
expect(
|
|
11
|
-
strings.base64UrlEncode(
|
|
12
|
-
"ladies and gentlemen we are floating in space",
|
|
13
|
-
),
|
|
14
|
-
).to.equal(
|
|
15
|
-
"bGFkaWVzIGFuZCBnZW50bGVtZW4gd2UgYXJlIGZsb2F0aW5nIGluIHNwYWNl",
|
|
16
|
-
);
|
|
17
|
-
expect(
|
|
18
|
-
strings.base64UrlDecode("cmlkZTogZHJlYW1zIGJ1cm4gZG93bg"),
|
|
19
|
-
).to.equal("ride: dreams burn down");
|
|
20
|
-
expect(
|
|
21
|
-
strings.base64ToBase64Url("qL8R4QIcQ/ZsRqOAbeRfcZhilN/MksRtDaErMA=="),
|
|
22
|
-
).to.equal("qL8R4QIcQ_ZsRqOAbeRfcZhilN_MksRtDaErMA");
|
|
23
|
-
expect(
|
|
24
|
-
strings.base64UrlToBase64("qL8R4QIcQ_ZsRqOAbeRfcZhilN_MksRtDaErMA"),
|
|
25
|
-
).to.equal("qL8R4QIcQ/ZsRqOAbeRfcZhilN/MksRtDaErMA==");
|
|
26
|
-
const buf = Buffer.alloc(13);
|
|
27
|
-
buf.writeUInt8(0x73, 0);
|
|
28
|
-
buf.writeUInt8(0x70, 1);
|
|
29
|
-
buf.writeUInt8(0x69, 2);
|
|
30
|
-
buf.writeUInt8(0x72, 3);
|
|
31
|
-
buf.writeUInt8(0x69, 4);
|
|
32
|
-
buf.writeUInt8(0x74, 5);
|
|
33
|
-
buf.writeUInt8(0x75, 6);
|
|
34
|
-
buf.writeUInt8(0x61, 7);
|
|
35
|
-
buf.writeUInt8(0x6c, 8);
|
|
36
|
-
buf.writeUInt8(0x69, 9);
|
|
37
|
-
buf.writeUInt8(0x7a, 10);
|
|
38
|
-
buf.writeUInt8(0x65, 11);
|
|
39
|
-
buf.writeUInt8(0x64, 12);
|
|
40
|
-
expect(
|
|
41
|
-
strings.base64UrlToBuffer("c3Bpcml0dWFsaXplZA").toString(),
|
|
42
|
-
).to.equal(buf.toString());
|
|
43
|
-
});
|
|
44
|
-
});
|
|
45
|
-
|
|
46
|
-
describe("#endsWith()", () => {
|
|
47
|
-
it("should operate normally", () => {
|
|
48
|
-
const str = "To be, or not to be, that is the question.";
|
|
49
|
-
expect(strings.endsWith(str, "question.")).to.be.true;
|
|
50
|
-
expect(strings.endsWith(str, "to be")).to.be.false;
|
|
51
|
-
expect(strings.endsWith(str, "to be", 19)).to.be.true;
|
|
52
|
-
expect(strings.endsWith(str, "question.", -19)).to.be.false;
|
|
53
|
-
});
|
|
54
|
-
it("should return false if position is negative", () => {
|
|
55
|
-
const str = "To be, or not to be, that is the question.";
|
|
56
|
-
expect(strings.endsWith(str, "question.", -19)).to.be.false;
|
|
57
|
-
});
|
|
58
|
-
it("should thow an error when the first or second argument is missing", () => {
|
|
59
|
-
expect(() => {
|
|
60
|
-
strings.endsWith();
|
|
61
|
-
}).to.throw(TypeError);
|
|
62
|
-
expect(() => {
|
|
63
|
-
strings.endsWith("");
|
|
64
|
-
}).to.throw(TypeError);
|
|
65
|
-
});
|
|
66
|
-
});
|
|
67
|
-
|
|
68
|
-
describe("#fromCharCode(num1[, ...[, numN]])", () => {
|
|
69
|
-
it("should return a string created from a sequence of UTF-16 code units", () => {
|
|
70
|
-
// https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String/fromCharCode
|
|
71
|
-
expect(strings.fromCharCode(65, 66, 67)).to.eql("ABC");
|
|
72
|
-
expect(strings.fromCharCode(0x2014)).to.eql("—");
|
|
73
|
-
expect(strings.fromCharCode(0x12014)).to.eql("—"); // The digit 1 is truncated and ignored.
|
|
74
|
-
});
|
|
75
|
-
});
|
|
76
|
-
|
|
77
|
-
describe("#isString(object)", () => {
|
|
78
|
-
it("should operate normally", () => {
|
|
79
|
-
// To enumerate the possibilites here, I used the possible return values
|
|
80
|
-
// of typeof as inspiration:
|
|
81
|
-
// https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/typeof
|
|
82
|
-
expect(strings.isString("")).to.be.true;
|
|
83
|
-
expect(strings.isString("frog")).to.be.true;
|
|
84
|
-
expect(strings.isString()).to.be.false;
|
|
85
|
-
expect(strings.isString(null)).to.be.false;
|
|
86
|
-
expect(strings.isString(false)).to.be.false;
|
|
87
|
-
expect(strings.isString(0)).to.be.false;
|
|
88
|
-
expect(
|
|
89
|
-
strings.isString(() => {
|
|
90
|
-
return "";
|
|
91
|
-
}),
|
|
92
|
-
).to.be.false;
|
|
93
|
-
expect(strings.isString({})).to.be.false;
|
|
94
|
-
});
|
|
95
|
-
});
|
|
96
|
-
|
|
97
|
-
describe("#pad(n, width, z)", () => {
|
|
98
|
-
it("should operate normally", () => {
|
|
99
|
-
expect(strings.pad("frog", 3)).to.equal("frog");
|
|
100
|
-
expect(strings.pad("frog", 4)).to.equal("frog");
|
|
101
|
-
expect(strings.pad("frog", 6)).to.equal("00frog");
|
|
102
|
-
expect(strings.pad(123456, 3)).to.equal("123456");
|
|
103
|
-
expect(strings.pad(123456, 6)).to.equal("123456");
|
|
104
|
-
expect(strings.pad(123456, 10)).to.equal("0000123456");
|
|
105
|
-
expect(strings.pad("frog", 3, 5)).to.equal("frog");
|
|
106
|
-
expect(strings.pad("frog", 4, 5)).to.equal("frog");
|
|
107
|
-
expect(strings.pad("frog", 6, 5)).to.equal("55frog");
|
|
108
|
-
expect(strings.pad(123456, 3, 5)).to.equal("123456");
|
|
109
|
-
expect(strings.pad(123456, 6, 5)).to.equal("123456");
|
|
110
|
-
expect(strings.pad(123456, 10, 5)).to.equal("5555123456");
|
|
111
|
-
});
|
|
112
|
-
it("should return the original string if width is not positive", () => {
|
|
113
|
-
expect(strings.pad("frog", 0)).to.equal("frog");
|
|
114
|
-
expect(strings.pad("frog", -3)).to.equal("frog");
|
|
115
|
-
expect(strings.pad("frog", -4)).to.equal("frog");
|
|
116
|
-
expect(strings.pad("frog", -6)).to.equal("frog");
|
|
117
|
-
});
|
|
118
|
-
it("should use a padding character of '0' when the padding character is the empty string", () => {
|
|
119
|
-
expect(strings.pad("frog", 3, "")).to.equal("frog");
|
|
120
|
-
expect(strings.pad("frog", 4, "")).to.equal("frog");
|
|
121
|
-
expect(strings.pad("frog", 6, "")).to.equal("00frog");
|
|
122
|
-
expect(strings.pad(123456, 3, "")).to.equal("123456");
|
|
123
|
-
expect(strings.pad(123456, 6, "")).to.equal("123456");
|
|
124
|
-
expect(strings.pad(123456, 10, "")).to.equal("0000123456");
|
|
125
|
-
});
|
|
126
|
-
it("should throw an error when the padding character's length is not 1", () => {
|
|
127
|
-
expect(() => {
|
|
128
|
-
strings.pad("frog", 6, 57);
|
|
129
|
-
}).to.throw(Error);
|
|
130
|
-
expect(() => {
|
|
131
|
-
strings.pad(123456, 10, 57);
|
|
132
|
-
}).to.throw(Error);
|
|
133
|
-
});
|
|
134
|
-
});
|
|
135
|
-
|
|
136
|
-
describe("#stripTags(str)", () => {
|
|
137
|
-
it("should operate normally", () => {
|
|
138
|
-
expect(strings.stripTags("frog")).to.equal("frog");
|
|
139
|
-
expect(
|
|
140
|
-
strings.stripTags('<html><>frog</html><span style=""> </span>'),
|
|
141
|
-
).to.equal("<>frog ");
|
|
142
|
-
});
|
|
143
|
-
it("should throw an error if any argument is null", () => {
|
|
144
|
-
expect(() => strings.stripTags()).to.throw(TypeError);
|
|
145
|
-
});
|
|
146
|
-
});
|
|
147
|
-
});
|