Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

merge: develop into main #6

Merged
merged 5 commits into from
Aug 29, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 9 additions & 0 deletions .all-contributorsrc
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,15 @@
"contributions": [
"features"
]
},
{
"login": "Glerme",
"name": "Guilherme Felipe",
"avatar_url": "https://avatars.githubusercontent.com/u/62507710?v=4",
"profile": "https://github.com/Glerme",
"contributions": [
"features"
]
}
],
"contributorsPerLine": 7,
Expand Down
10 changes: 8 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -76,13 +76,19 @@ These are just some of the ways you can contribute to the project read the [CONT
<table>
<tr>
<td align="center"><a href="https://wesleyaraujo.dev/"><img src="https://avatars.githubusercontent.com/u/89321125?v=4?s=100" width="100px;" alt=""/><br /><sub><b>Wesley Araújo</b></sub></a><br /></td>
<td align="center"><a href="https://github.com/Patryck-Silva"><img src="https://avatars.githubusercontent.com/u/41785386?v=4" width="100px;" alt=""/><br /><sub><b>Patryck Silva</b></sub></a><br /></td>
<td align="center"><a href="https://github.com/joaoianuci"><img src="https://avatars.githubusercontent.com/u/42063789?v=4" width="100px;" alt=""/><br /><sub><b>João Vitor Ianuci</b></sub></a><br /></td>
</tr>
</table>

## ✨ Contributors

<table>
<tr>
<td align="center"><a href="https://github.com/Patryck-Silva"><img src="https://avatars.githubusercontent.com/u/41785386?v=4" width="100px;" alt=""/><br /><sub><b>Patryck Silva</b></sub></a><br /></td>
<td align="center"><a href="https://github.com/joaoianuci"><img src="https://avatars.githubusercontent.com/u/42063789?v=4" width="100px;" alt=""/><br /><sub><b>João Vitor Ianuci</b></sub></a><br /></td>
<td align="center"><a href="https://github.com/Glerme"><img src="https://avatars.githubusercontent.com/u/62507710?v=4" width="100px;" alt=""/><br /><sub><b>Guilherme Felipe</b></sub></a><br /></td>
</tr>
</table>

Thanks goes to these wonderful people ([emoji key](https://allcontributors.org/docs/en/emoji-key)):

<!-- ALL-CONTRIBUTORS-LIST:START - Do not remove or modify this section -->
Expand Down
15 changes: 15 additions & 0 deletions docs/react.md
Original file line number Diff line number Diff line change
Expand Up @@ -64,3 +64,18 @@ import { useNow } from "utils-react";
const now = useNow();
console.log(now);
```

## useToggle

Toggle a boolean.

```js
import { useToggle } from "utils-react";

// called in a component
const { toggleValue, handleToggleValue } = useToggle();

console.log(toggleValue); // false
handleToggleValue();
console.log(toggleValue); // true
```
10 changes: 10 additions & 0 deletions docs/vanilla.md
Original file line number Diff line number Diff line change
Expand Up @@ -279,3 +279,13 @@ import { removeStorage } from "utils-react";

removeStorage("name");
```

## getNavigatorCurrentLocation

Get the current location of the navigator.

```js
import { getNavigatorCurrentLocation } from "utils-react";

getNavigatorCurrentLocation().then((location) => console.log(location));
```
2 changes: 2 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -44,12 +44,14 @@
"homepage": "https://github.com/wesleyara/utils-react#readme",
"dependencies": {
"axios": "^1.2.0",
"jsdom": "^22.1.0",
"react": "^18.2.0",
"rimraf": "^3.0.2"
},
"devDependencies": {
"@commitlint/cli": "^17.3.0",
"@commitlint/config-conventional": "^17.1.0",
"@types/jsdom": "^21.1.2",
"@types/node": "^18.11.9",
"@types/react": "^18.0.25",
"@typescript-eslint/eslint-plugin": "^5.45.0",
Expand Down
4 changes: 4 additions & 0 deletions src/@types/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
export interface IGetNavigatorCurrentLocation {
successCallback: PositionCallback;
errorCallback: PositionErrorCallback;
}
49 changes: 48 additions & 1 deletion src/__test__/index.test.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,8 @@
import { describe, expect, it } from "vitest";
/**
* @jest-environment jsdom
*/

import { describe, expect, it, vitest } from "vitest";

import {
upperFirst,
Expand All @@ -19,6 +23,7 @@ import {
generateNullArray,
generateRandomString,
generateRandomColor,
getNavigatorCurrentLocation,
} from "../index";

describe("all methods in the package", () => {
Expand Down Expand Up @@ -122,4 +127,46 @@ describe("all methods in the package", () => {
it("generate a random color", () => {
expect(generateRandomColor()).toBeTypeOf("string");
});

it("get current location", async () => {
const mockSuccessCallback = vitest.fn();
const mockErrorCallback = vitest.fn();

if (!global.navigator.geolocation) {
return;
}

global.navigator.geolocation.getCurrentPosition = vitest.fn(success => {
success({
coords: {
latitude: 10,
longitude: 20,
accuracy: 1,
altitude: null,
altitudeAccuracy: null,
heading: null,
speed: null,
},
timestamp: 123,
});
});

const result = await getNavigatorCurrentLocation({
successCallback: mockSuccessCallback,
errorCallback: mockErrorCallback,
});

expect(result).toEqual({
coords: {
latitude: 10,
longitude: 20,
accuracy: 1,
altitude: null,
altitudeAccuracy: null,
heading: null,
speed: null,
},
timestamp: 123,
});
});
});
13 changes: 13 additions & 0 deletions src/modules/index.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
import axios from "axios";

import { IGetNavigatorCurrentLocation } from "../@types";

export const upperFirst = (str: string) => {
return str.charAt(0).toUpperCase() + str.slice(1);
};
Expand Down Expand Up @@ -31,7 +33,7 @@
return str.split("").reverse().join("");
};

export const arrayEquals = (a: any[], b: any[]) => {

Check warning on line 36 in src/modules/index.ts

View workflow job for this annotation

GitHub Actions / lint (16.x)

Unexpected any. Specify a different type

Check warning on line 36 in src/modules/index.ts

View workflow job for this annotation

GitHub Actions / lint (16.x)

Unexpected any. Specify a different type

Check warning on line 36 in src/modules/index.ts

View workflow job for this annotation

GitHub Actions / lint (16.x)

Unexpected any. Specify a different type

Check warning on line 36 in src/modules/index.ts

View workflow job for this annotation

GitHub Actions / lint (16.x)

Unexpected any. Specify a different type
if (!Array.isArray(a) || !Array.isArray(b) || a.length !== b.length) {
return false;
}
Expand All @@ -45,14 +47,14 @@
return true;
};

export const arrayToObject = (array: any[], key: string) => {

Check warning on line 50 in src/modules/index.ts

View workflow job for this annotation

GitHub Actions / lint (16.x)

Unexpected any. Specify a different type

Check warning on line 50 in src/modules/index.ts

View workflow job for this annotation

GitHub Actions / lint (16.x)

Unexpected any. Specify a different type
return array.reduce((obj, item) => {
obj[item[key]] = item;
return obj;
}, {});
};

export const removeArrayItemByValue = (array: any[], value: any) => {

Check warning on line 57 in src/modules/index.ts

View workflow job for this annotation

GitHub Actions / lint (16.x)

Unexpected any. Specify a different type

Check warning on line 57 in src/modules/index.ts

View workflow job for this annotation

GitHub Actions / lint (16.x)

Unexpected any. Specify a different type

Check warning on line 57 in src/modules/index.ts

View workflow job for this annotation

GitHub Actions / lint (16.x)

Unexpected any. Specify a different type

Check warning on line 57 in src/modules/index.ts

View workflow job for this annotation

GitHub Actions / lint (16.x)

Unexpected any. Specify a different type
const index = array.indexOf(value);
if (index > -1) {
array.splice(index, 1);
Expand All @@ -61,7 +63,7 @@
return array;
};

export const removeArrayItemByIndex = (array: any[], index: number) => {

Check warning on line 66 in src/modules/index.ts

View workflow job for this annotation

GitHub Actions / lint (16.x)

Unexpected any. Specify a different type

Check warning on line 66 in src/modules/index.ts

View workflow job for this annotation

GitHub Actions / lint (16.x)

Unexpected any. Specify a different type
if (index > -1) {
array.splice(index, 1);
}
Expand All @@ -69,7 +71,7 @@
return array;
};

export const objectToArray = (obj: any) => {

Check warning on line 74 in src/modules/index.ts

View workflow job for this annotation

GitHub Actions / lint (16.x)

Unexpected any. Specify a different type

Check warning on line 74 in src/modules/index.ts

View workflow job for this annotation

GitHub Actions / lint (16.x)

Unexpected any. Specify a different type
return Object.keys(obj).map(key => obj[key]);
};

Expand Down Expand Up @@ -98,7 +100,7 @@
};

export const smartLog = (
value: any,

Check warning on line 103 in src/modules/index.ts

View workflow job for this annotation

GitHub Actions / lint (16.x)

Unexpected any. Specify a different type

Check warning on line 103 in src/modules/index.ts

View workflow job for this annotation

GitHub Actions / lint (16.x)

Unexpected any. Specify a different type
currentFunction: Function,
label?: string,
) => {
Expand All @@ -110,11 +112,11 @@
);
};

export function isEmptyObject(obj: any) {

Check warning on line 115 in src/modules/index.ts

View workflow job for this annotation

GitHub Actions / lint (16.x)

Unexpected any. Specify a different type

Check warning on line 115 in src/modules/index.ts

View workflow job for this annotation

GitHub Actions / lint (16.x)

Unexpected any. Specify a different type
return JSON.stringify(obj) === "{}";
}

export const shuffleArray = (array: any) => {

Check warning on line 119 in src/modules/index.ts

View workflow job for this annotation

GitHub Actions / lint (16.x)

Unexpected any. Specify a different type

Check warning on line 119 in src/modules/index.ts

View workflow job for this annotation

GitHub Actions / lint (16.x)

Unexpected any. Specify a different type
for (let i = array.length - 1; i > 0; i--) {
const j = Math.floor(Math.random() * (i + 1));
const temp = array[i];
Expand Down Expand Up @@ -182,3 +184,14 @@
export const generateRandomColor = () => {
return "#" + Math.floor(Math.random() * 16777215).toString(16);
};

export const getNavigatorCurrentLocation = async ({
successCallback,
errorCallback,
}: IGetNavigatorCurrentLocation) => {
const { coords, timestamp }: GeolocationPosition = await new Promise(() => {
navigator.geolocation.getCurrentPosition(successCallback, errorCallback);
});

return { coords, timestamp };
};
18 changes: 18 additions & 0 deletions src/modules/react/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -71,3 +71,21 @@ export const useNow = () => {

return now;
};

export const useToggle = (
initialValue: boolean,
): {
toggleValue: boolean;
handleToggleValue: () => void;
} => {
const [toggleValue, setToggleValue] = useState(initialValue);

const handleToggleValue = () => {
setToggleValue(state => !state);
};

return {
toggleValue,
handleToggleValue,
};
};
Loading