diff --git a/snippets/isNegativeZero.md b/snippets/isNegativeZero.md new file mode 100644 index 000000000..e4d4df5a0 --- /dev/null +++ b/snippets/isNegativeZero.md @@ -0,0 +1,14 @@ +### isNegativeZero + +Checks if the given value is a negative zero `-0`. + +Checks whether a passed value is equal to `0` and if one devided by the value equals `-Infinity`. + +```js +const isNegativeZero = val => val === 0 && 1 / val === -Infinity; +``` + +```js +isNegativeZero(-0); // true +isNegativeZero(0); // false +``` diff --git a/tag_database b/tag_database index aaa61a47e..436fb8869 100644 --- a/tag_database +++ b/tag_database @@ -145,6 +145,7 @@ isEmpty:type,array,object,string,beginner isEven:math,beginner isFunction:type,function,beginner isLowerCase:string,utility,beginner +isNegativeZero:math,utility,beginner isNil:type,beginner isNull:type,beginner isNumber:type,math,beginner diff --git a/test/isNegativeZero.test.js b/test/isNegativeZero.test.js new file mode 100644 index 000000000..963693a43 --- /dev/null +++ b/test/isNegativeZero.test.js @@ -0,0 +1,15 @@ +const expect = require('expect'); +const { isNegativeZero } = require('._30s.js'); + +test('isNegativeZero is a Function', () => { + expect(isNegativeZero).toBeInstanceOf(Function); +}); +test('0 is not a -0', () => { + expect(isNegativeZero(0)).toBeFalsy(); +}); +test('-0 is a negative zero', () => { + expect(isNegativeZero(-0)).toBeTruthy(); +}); +test('1 is not a negative zero', () => { + expect(isNegativeZero(1)).toBeFalsy(); +});