Skip to content

Commit

Permalink
[chore]: logicNot organized
Browse files Browse the repository at this point in the history
  • Loading branch information
changeelog committed Jun 2, 2024
1 parent df3d063 commit 3e9f0b4
Show file tree
Hide file tree
Showing 7 changed files with 118 additions and 30 deletions.
7 changes: 7 additions & 0 deletions hooks/Math/logicNot/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
# logicNot

## 0.0.1

### Patch Changes

- [`24bff3d`](https://github.com/changeelog/react-hooks/commit/24bff3d24011de5955505c33d973989cc5786879#diff-df7ad740052307aebbbdde0d86acbb03b4a98ae5518d68b8620cc2a0796f4a54)- Introduced logicNot
19 changes: 19 additions & 0 deletions hooks/Math/logicNot/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
# logicNot

`NOT` conditions for refs

Please refer to the [documentation](#) for more information.

## Installation

```bash
npx changeelog/reactuse@latest add logicNot
```

## Contribution

Yes please! See the [contributing guidelines](#) for more details!

## License

This project is licensed under the terms of the [MIT License](/LICENSE)
42 changes: 42 additions & 0 deletions hooks/Math/logicNot/__tests__/logicNot.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
import { logicNot } from '../logicNot'

describe('logicNot', () => {
test('should return false when input is true', () => {
expect(logicNot(true)).toBe(false)
})

test('should return true when input is false', () => {
expect(logicNot(false)).toBe(true)
})

test('should return false when getter returns true', () => {
const getter = () => true
expect(logicNot(getter)).toBe(false)
})

test('should return true when getter returns false', () => {
const getter = () => false
expect(logicNot(getter)).toBe(true)
})

test('should throw an error for non-boolean and non-function input', () => {
const invalidInput = 123
expect(() => logicNot(invalidInput as any)).toThrow(
'logicNot: Expected a boolean or a function that returns a boolean.'
)
})

test('should throw an error when function does not return a boolean', () => {
const invalidGetter = () => 'hello'
expect(() => logicNot(invalidGetter as any)).toThrow(
'logicNot: Expected a boolean or a function that returns a boolean.'
)
})

test('should preserve the boolean type', () => {
const result1: boolean = logicNot(true)
const result2: boolean = logicNot(() => false)
expect(result1).toBe(false)
expect(result2).toBe(true)
})
})
1 change: 1 addition & 0 deletions hooks/Math/logicNot/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export * from './logicNot'
40 changes: 40 additions & 0 deletions hooks/Math/logicNot/logicNot.demo.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
import React, { useState } from 'react'
import { logicNot } from './logicNot'

const LogicNotDemo = () => {
const [value, setValue] = useState(true)
const [getterValue, setGetterValue] = useState(true)

const handleChange = (event: React.ChangeEvent<HTMLInputElement>) => {
setValue(event.target.checked)
}

const handleGetterChange = (event: React.ChangeEvent<HTMLInputElement>) => {
setGetterValue(event.target.checked)
}

const notValue = logicNot(value)
const notGetterValue = logicNot(() => getterValue)

return (
<div>
<h1>Logic Not Demo</h1>
<label>
Input:
<input type="checkbox" checked={value} onChange={handleChange} />
</label>
<p>NOT: {notValue.toString()}</p>
<label>
Getter Input:
<input
type="checkbox"
checked={getterValue}
onChange={handleGetterChange}
/>
</label>
<p>NOT Getter: {notGetterValue.toString()}</p>
</div>
)
}

export default LogicNotDemo
10 changes: 9 additions & 1 deletion hooks/Math/logicNot/logicNot.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,16 @@ type MaybeRefOrGetter<T> = T | (() => T)
/**
* `NOT` conditions for refs
*
* @param {MaybeRefOrGetter<boolean>} v - The ref or getter to be NOTed.
* This utility returns the negation of a boolean value or a getter function that returns a boolean.
*
* @param {MaybeRefOrGetter<boolean>} value - The ref or getter to be NOTed.
* @returns {[boolean, (newValue: boolean) => void]} - An array containing the result of the NOT operation and a function to update the value.
*
* @example
* const [isTrue, setTrue] = logicNot(true);
* console.log(isTrue); // Output: false;
* setTrue(false);
* console.log(istTrue); // Output: true;
*/
export function logicNot<T extends boolean>(value: MaybeRefOrGetter<T>): T {
const resolvedValue = typeof value === 'function' ? value() : value
Expand Down
29 changes: 0 additions & 29 deletions hooks/Math/usePrecision.ts

This file was deleted.

0 comments on commit 3e9f0b4

Please sign in to comment.