Skip to content

Commit 4ea235e

Browse files
committed
add isPowerOfTwo
1 parent 0e5e760 commit 4ea235e

10 files changed

Lines changed: 149 additions & 20 deletions

File tree

CHANGELOG.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,7 @@
1+
# 2.12.0 (September 13th 2020)
2+
3+
- Add IsPowerOfTwo() method
4+
15
# 2.11.0 (September 11th 2020)
26

37
- IE9 compatibility for iife version

README.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -124,6 +124,8 @@ isPositive(number: any): boolean {}
124124

125125
isNegative(number: any): boolean {}
126126

127+
isPowerOfTwo(number: any): boolean {}
128+
127129
isRegex(regex: any, typeCheck: boolean = true): boolean {}
128130

129131
isEmail(email: any): boolean {}

dist/ch.cjs.js

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -208,6 +208,9 @@ function isPositive(number) {
208208
function isNegative(number) {
209209
return number < 0 ? true : false;
210210
}
211+
function isPowerOfTwo(number) {
212+
return isInteger(number) && !isOrigin(number) && (number & (number - 1)) === 0;
213+
}
211214
function isEmail(email) {
212215
var regex = /^(?=[a-z0-9@.!$%&'*+\/=?^_{|}~-]{6,254}$)(?=[a-z0-9.!#$%&'*+\/=?^_{|}~-]{1,64}@)[a-z0-9!#$%&'*+\/=?^{|}~]+(?:[\._-][a-z0-9!#$%&'*+\/=?^{|}~]+)*@(?:(?=[a-z0-9-]{1,63}\.)[a-z0-9](?:[a-z0-9-]*[a-z0-9])?\.)+(?=[a-z0-9-]{2,63}$)[a-z0-9](?:[a-z0-9-]*[a-z0-9])?$/;
213216
return regex.test(String(email).toLowerCase());
@@ -236,5 +239,6 @@ exports.isObject = isObject;
236239
exports.isOdd = isOdd;
237240
exports.isOrigin = isOrigin;
238241
exports.isPositive = isPositive;
242+
exports.isPowerOfTwo = isPowerOfTwo;
239243
exports.isRegex = isRegex;
240244
exports.isString = isString;

dist/ch.d.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,7 @@ declare function isOdd(number: any): boolean;
4242
declare function isOrigin(number: any): boolean;
4343
declare function isPositive(number: any): boolean;
4444
declare function isNegative(number: any): boolean;
45+
declare function isPowerOfTwo(number: any): boolean;
4546
declare function isEmail(email: any): boolean;
4647
declare function isIpAddress(ipAddress: any): boolean;
47-
export { isBoolean, isJson, isFunction, isObject, isArray, isAscii, isInteger, isFloat, isNumber, isString, isHtmlElement, isHtmlEventAttribute, isNode, isEven, isOdd, isOrigin, isPositive, isNegative, isRegex, isEmail, isIpAddress };
48+
export { isBoolean, isJson, isFunction, isObject, isArray, isAscii, isInteger, isFloat, isNumber, isString, isHtmlElement, isHtmlEventAttribute, isNode, isEven, isOdd, isOrigin, isPositive, isNegative, isPowerOfTwo, isRegex, isEmail, isIpAddress };

dist/ch.iife.js

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -242,6 +242,10 @@ var Ch = (function (exports) {
242242
return number < 0 ? true : false;
243243
}
244244

245+
function isPowerOfTwo(number) {
246+
return isInteger(number) && !isOrigin(number) && (number & number - 1) === 0;
247+
}
248+
245249
function isEmail(email) {
246250
var regex = /^(?=[a-z0-9@.!$%&'*+\/=?^_{|}~-]{6,254}$)(?=[a-z0-9.!#$%&'*+\/=?^_{|}~-]{1,64}@)[a-z0-9!#$%&'*+\/=?^{|}~]+(?:[\._-][a-z0-9!#$%&'*+\/=?^{|}~]+)*@(?:(?=[a-z0-9-]{1,63}\.)[a-z0-9](?:[a-z0-9-]*[a-z0-9])?\.)+(?=[a-z0-9-]{2,63}$)[a-z0-9](?:[a-z0-9-]*[a-z0-9])?$/;
247251
return regex.test(String(email).toLowerCase());
@@ -271,6 +275,7 @@ var Ch = (function (exports) {
271275
exports.isOdd = isOdd;
272276
exports.isOrigin = isOrigin;
273277
exports.isPositive = isPositive;
278+
exports.isPowerOfTwo = isPowerOfTwo;
274279
exports.isRegex = isRegex;
275280
exports.isString = isString;
276281

dist/ch.iife.min.js

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

dist/ch.js

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -204,6 +204,9 @@ function isPositive(number) {
204204
function isNegative(number) {
205205
return number < 0 ? true : false;
206206
}
207+
function isPowerOfTwo(number) {
208+
return isInteger(number) && !isOrigin(number) && (number & (number - 1)) === 0;
209+
}
207210
function isEmail(email) {
208211
var regex = /^(?=[a-z0-9@.!$%&'*+\/=?^_{|}~-]{6,254}$)(?=[a-z0-9.!#$%&'*+\/=?^_{|}~-]{1,64}@)[a-z0-9!#$%&'*+\/=?^{|}~]+(?:[\._-][a-z0-9!#$%&'*+\/=?^{|}~]+)*@(?:(?=[a-z0-9-]{1,63}\.)[a-z0-9](?:[a-z0-9-]*[a-z0-9])?\.)+(?=[a-z0-9-]{2,63}$)[a-z0-9](?:[a-z0-9-]*[a-z0-9])?$/;
209212
return regex.test(String(email).toLowerCase());
@@ -212,4 +215,4 @@ function isIpAddress(ipAddress) {
212215
var regex = /^(?:(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.){3}(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)$/;
213216
return regex.test(ipAddress);
214217
}
215-
export { isBoolean, isJson, isFunction, isObject, isArray, isAscii, isInteger, isFloat, isNumber, isString, isHtmlElement, isHtmlEventAttribute, isNode, isEven, isOdd, isOrigin, isPositive, isNegative, isRegex, isEmail, isIpAddress };
218+
export { isBoolean, isJson, isFunction, isObject, isArray, isAscii, isInteger, isFloat, isNumber, isString, isHtmlElement, isHtmlEventAttribute, isNode, isEven, isOdd, isOrigin, isPositive, isNegative, isPowerOfTwo, isRegex, isEmail, isIpAddress };

package.json

Lines changed: 9 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "@lcluber/chjs",
3-
"version": "2.11.0",
3+
"version": "2.12.0",
44
"description": "Type checking library for Javascript",
55
"keywords": [
66
"type checking"
@@ -33,33 +33,25 @@
3333
"banner": "cat license.js build/es6/ch.js > dist/ch.js && cat license.js build/ch.d.ts > dist/ch.d.ts && cat license.js build/ch.iife.js > dist/ch.iife.js && cat license.js build/ch.iife.min.js > dist/ch.iife.min.js && cat license.js build/ch.cjs.js > dist/ch.cjs.js",
3434
"test": "jest --coverage"
3535
},
36-
"husky": {
37-
"hooks": {
38-
"pre-commit": "npm run test",
39-
"commit-msg": "",
40-
"pre-push": ""
41-
}
42-
},
4336
"files": [
4437
"dist/"
4538
],
4639
"dependencies": {},
4740
"devDependencies": {
48-
"@babel/core": "7.9.0",
49-
"@babel/preset-env": "7.9.0",
50-
"@rollup/plugin-commonjs": "11.0.2",
51-
"@rollup/plugin-node-resolve": "7.1.1",
41+
"@babel/core": "7.11.6",
42+
"@babel/preset-env": "7.11.5",
43+
"@rollup/plugin-commonjs": "15.0.0",
44+
"@rollup/plugin-node-resolve": "9.0.0",
5245
"babel-jest": "24.9.0",
5346
"core-js": "3.6.4",
54-
"husky": "2.7.0",
5547
"jest": "24.9.0",
5648
"rimraf": "3.0.2",
5749
"rollup": "2.3.3",
58-
"rollup-plugin-analyzer": "3.2.2",
50+
"rollup-plugin-analyzer": "3.3.0",
5951
"rollup-plugin-babel": "4.4.0",
60-
"rollup-plugin-progress": "1.1.1",
61-
"rollup-plugin-size-snapshot": "0.11.0",
62-
"rollup-plugin-sizes": "1.0.2",
52+
"rollup-plugin-progress": "1.1.2",
53+
"rollup-plugin-size-snapshot": "0.12.0",
54+
"rollup-plugin-sizes": "1.0.3",
6355
"tslint": "5.20.1",
6456
"typescript": "4.0.2",
6557
"uglify-js": "3.10.4"

src/ch.ts

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -198,6 +198,10 @@ function isNegative(number: any): boolean {
198198
return number < 0 ? true : false;
199199
}
200200

201+
function isPowerOfTwo(number: any): boolean {
202+
return isInteger(number) && !isOrigin(number) && (number & (number - 1)) === 0;
203+
}
204+
201205
function isEmail(email: any): boolean {
202206
var regex = /^(?=[a-z0-9@.!$%&'*+\/=?^_{|}~-]{6,254}$)(?=[a-z0-9.!#$%&'*+\/=?^_{|}~-]{1,64}@)[a-z0-9!#$%&'*+\/=?^{|}~]+(?:[\._-][a-z0-9!#$%&'*+\/=?^{|}~]+)*@(?:(?=[a-z0-9-]{1,63}\.)[a-z0-9](?:[a-z0-9-]*[a-z0-9])?\.)+(?=[a-z0-9-]{2,63}$)[a-z0-9](?:[a-z0-9-]*[a-z0-9])?$/;
203207
return regex.test(String(email).toLowerCase());
@@ -227,6 +231,7 @@ export {
227231
isOrigin,
228232
isPositive,
229233
isNegative,
234+
isPowerOfTwo,
230235
isRegex,
231236
isEmail,
232237
isIpAddress

tests/ispoweroftwo.test.js

Lines changed: 113 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,113 @@
1+
import { isPowerOfTwo } from "../dist/ch";
2+
3+
test("sends null to isPowerOfTwo", () => {
4+
expect(isPowerOfTwo(null)).toBe(false);
5+
});
6+
7+
test("sends true to isPowerOfTwo", () => {
8+
expect(isPowerOfTwo(true)).toBe(false);
9+
});
10+
11+
test("sends false to isPowerOfTwo", () => {
12+
expect(isPowerOfTwo(false)).toBe(false);
13+
});
14+
15+
test("sends string to isPowerOfTwo", () => {
16+
expect(isPowerOfTwo("string")).toBe(false);
17+
});
18+
19+
test("sends 2 integer to isPowerOfTwo", () => {
20+
expect(isPowerOfTwo(2)).toBe(true);
21+
});
22+
23+
test("sends 16 integer to isPowerOfTwo", () => {
24+
expect(isPowerOfTwo(16)).toBe(true);
25+
});
26+
27+
test("sends 1 integer to isPowerOfTwo", () => {
28+
expect(isPowerOfTwo(1)).toBe(true);
29+
});
30+
31+
test("sends zero to isPowerOfTwo", () => {
32+
expect(isPowerOfTwo(0)).toBe(false);
33+
});
34+
35+
test("sends positive float to isPowerOfTwo", () => {
36+
expect(isPowerOfTwo(1.1)).toBe(false);
37+
});
38+
39+
test("sends negative odd integer to isPowerOfTwo", () => {
40+
expect(isPowerOfTwo(-1)).toBe(false);
41+
});
42+
43+
test("sends negative even integer to isPowerOfTwo", () => {
44+
expect(isPowerOfTwo(-2)).toBe(false);
45+
});
46+
47+
test("sends negative float to isPowerOfTwo", () => {
48+
expect(isPowerOfTwo(-1.1)).toBe(false);
49+
});
50+
51+
test("sends object to isPowerOfTwo", () => {
52+
expect(isPowerOfTwo({})).toBe(false);
53+
});
54+
55+
test("sends empty array to isPowerOfTwo", () => {
56+
expect(isPowerOfTwo([])).toBe(false);
57+
});
58+
59+
test("sends array to isPowerOfTwo", () => {
60+
expect(isPowerOfTwo(["white", "grey", "black"])).toBe(false);
61+
});
62+
63+
var json = `{
64+
"actor": {
65+
"name": "Tom Cruise",
66+
"age": 56,
67+
"Born At": "Syracuse, NY",
68+
"Birthdate": "July 3 1962",
69+
"photo": "https://jsonformatter.org/img/tom-cruise.jpg"
70+
}
71+
}`;
72+
73+
test("sends json to isPowerOfTwo", () => {
74+
expect(isPowerOfTwo(json)).toBe(false);
75+
});
76+
77+
var invalidjson = `{
78+
"actor: {
79+
"name": "Tom Cruise",
80+
"age": 56
81+
"Born At": "Syracuse, NY",
82+
"Birthdate": "July 3 1962",
83+
"photo": "https://jsonformatter.org/img/tom-cruise.jpg"
84+
}
85+
}`;
86+
87+
test("sends invalid json to isPowerOfTwo", () => {
88+
expect(isPowerOfTwo(invalidjson)).toBe(false);
89+
});
90+
91+
function testFunction() {
92+
console.log("function");
93+
}
94+
95+
test("sends function to isPowerOfTwo", () => {
96+
expect(isPowerOfTwo(testFunction)).toBe(false);
97+
});
98+
99+
var para = document.createElement("p");
100+
101+
test("sends htmlElement to isPowerOfTwo", () => {
102+
expect(isPowerOfTwo(para)).toBe(false);
103+
});
104+
105+
var node = document.createTextNode("new node");
106+
107+
test("sends node to isPowerOfTwo", () => {
108+
expect(isPowerOfTwo(node)).toBe(false);
109+
});
110+
111+
test("sends regex to isPowerOfTwo", () => {
112+
expect(isPowerOfTwo(/ab+c/i)).toBe(false);
113+
});

0 commit comments

Comments
 (0)