Skip to content

Commit

Permalink
Merge pull request #95 from 201flaviosilva-labs/44-get-browser-data
Browse files Browse the repository at this point in the history
Get browser data
  • Loading branch information
201flaviosilva committed Oct 20, 2022
2 parents c7bdfec + f5852eb commit 8eb3cbf
Show file tree
Hide file tree
Showing 22 changed files with 249 additions and 3 deletions.
6 changes: 6 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,11 @@ This file was based on [this template](https://gist.github.com/juampynr/4c18214a
- [Get Position With Angle Distance](https://github.com/201flaviosilva-labs/javascript-utils/commit/80b83a7f35508d9997fb95bf2b6af066b8b4e553);
- [Is Falsy](https://github.com/201flaviosilva-labs/javascript-utils/commit/25b13f2b7fc66c01e6709ac8adee2e57f79fc5fd) #84;
- [Is Truthy](https://github.com/201flaviosilva-labs/javascript-utils/commit/25b13f2b7fc66c01e6709ac8adee2e57f79fc5fd) #84;
- [getBrowser](https://github.com/201flaviosilva-labs/javascript-utils/pull/95/commits/c81f718f9497adc9f69f057950f7594b12dfa52d) - Returns the browser name #44;
- [getBrowserVersion](https://github.com/201flaviosilva-labs/javascript-utils/pull/95/commits/c81f718f9497adc9f69f057950f7594b12dfa52d) - Returns the browser version #44;
- [getOS](https://github.com/201flaviosilva-labs/javascript-utils/pull/95/commits/c81f718f9497adc9f69f057950f7594b12dfa52d) - Returns the OS name #44;
- [getOSVersion](https://github.com/201flaviosilva-labs/javascript-utils/pull/95/commits/c81f718f9497adc9f69f057950f7594b12dfa52d) - Returns the OS version #44;
- [isMobile](https://github.com/201flaviosilva-labs/javascript-utils/pull/95/commits/c81f718f9497adc9f69f057950f7594b12dfa52d) - Check if is in a mobile device #44;

### Changed
- [Add more examples to the functions](https://github.com/201flaviosilva-labs/javascript-utils/commit/06c75f5b84da32b9af4521eab48afc3d4982a8aa);
Expand All @@ -34,6 +39,7 @@ This file was based on [this template](https://gist.github.com/juampynr/4c18214a
- [Only export `getDate` function from `getDate` file](https://github.com/201flaviosilva-labs/javascript-utils/commit/2c0e9f8d293c68f36e9297b70c18321678e40921);
- [Removed `nand`, `nor`, `not` and `xnor`](https://github.com/201flaviosilva-labs/javascript-utils/commit/2473d089c7a699650abfce108425ee0d479ce7e7) #88;
- [Move array functions to a folder](https://github.com/201flaviosilva-labs/javascript-utils/pull/92/commits/237cbf42f22131dd83c5126107f21b32ce33e232) #89;
- [Move date function to device folder/namespace](https://github.com/201flaviosilva-labs/javascript-utils/pull/95/commits/c81f718f9497adc9f69f057950f7594b12dfa52d);


## [1.2.9] - 02-09-2022
Expand Down
3 changes: 3 additions & 0 deletions src/Array/allEqual.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,5 +14,8 @@
*
* @param {any} arr - the array to check all elements
* @returns {boolean} true if all elements of the array ara equal
*
* @function allEqual
* @memberof Array
*/
export function allEqual(arr) { return arr.every(v => JSON.stringify(v) === JSON.stringify(arr[0])); };
3 changes: 3 additions & 0 deletions src/Array/choice.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,9 @@ import { randomInt } from "../randomNumber";
*
* @param {any[]} array - the array to select a random item
* @returns {any}
*
* @function choice
* @memberof Array
*/
export function choice(array) {
return array[randomInt(array.length)];
Expand Down
6 changes: 6 additions & 0 deletions src/Array/findBigObject.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,9 @@
* @param {string} prop - The property to find the biggest element
* @param {boolean} [returnOnlyValue=false] - If true only returns the value of the given property with the biggest value
* @returns {any|number} - The biggest element in the array
*
* @function findBigObject
* @memberof Array
*/
export function findBigObject(array, prop, returnOnlyValue = false) {
const biggest = Math.max(...array.map(o => o[prop]));
Expand All @@ -24,6 +27,9 @@ export function findBigObject(array, prop, returnOnlyValue = false) {
* @param {Object[]} array - The array to search
* @param {string} prop - The property to find the biggest element
* @returns {Object} - The biggest element in the array
*
* @function findBigObjectDeprecated
* @memberof Array
* @deprecated
*/
export function findBigObjectDeprecated(array, prop) {
Expand Down
6 changes: 6 additions & 0 deletions src/Array/findLowObject.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,9 @@
* @param {string} prop - The property to find the lowest element
* @param {boolean} [returnOnlyValue=false] - If true only returns the value of the given property with the lowest value
* @returns {Object} - The lowest element in the array
*
* @function findLowObject
* @memberof Array
*/

export function findLowObject(array, prop, returnOnlyValue = false) {
Expand All @@ -25,6 +28,9 @@ export function findLowObject(array, prop, returnOnlyValue = false) {
* @param {Object[]} array - The array to search
* @param {string} prop - The property to find the lowest element
* @returns {Object} - The lowest element in the array
*
* @function findLowObjectDeprecated
* @memberof Array
* @deprecated
*/
export function findLowObjectDeprecated(array, prop) {
Expand Down
5 changes: 5 additions & 0 deletions src/Array/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,11 @@ import { sortAscendingObject } from "./sortAscendingObject.js";
import { sortDescending } from "./sortDescending.js";
import { sortDescendingObject } from "./sortDescendingObject.js";

/**
* Functions utils for arrays
*
* @namespace Array
*/
export {
allEqual,
choice,
Expand Down
3 changes: 3 additions & 0 deletions src/Array/isSorted.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,9 @@
*
* @param {any} arr - the array to check
* @returns {boolean} true if the array is sorted
*
* @function isSorted
* @memberof Array
*/
export function isSorted(arr = []) {
for (let i = 0; i < arr.length - 1; i++) {
Expand Down
6 changes: 6 additions & 0 deletions src/Array/move.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,9 @@
* @param {any[]} array - The array to move
* @param {number} times - The number of times to move the array
* @returns {any[]}
*
* @function moveLeft
* @memberof Array
*/
export function moveLeft(array = [], times = 1) {
for (let t = 0; t < times; t++) {
Expand Down Expand Up @@ -38,6 +41,9 @@ export function moveLeft(array = [], times = 1) {
* @param {any[]} array - The array to move
* @param {number} times - The number of times to move the array
* @returns {any[]}
*
* @function moveRight
* @memberof Array
*/
export function moveRight(array = [], times = 1) {
for (let t = 0; t < times; t++) {
Expand Down
3 changes: 3 additions & 0 deletions src/Array/shuffle.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,9 @@ import { clone } from "../clone";
* @param {any[]} array - the array with the items to randomize
* @param {boolean} mutateOriginal - the array with the items to randomize
* @returns {any[]}
*
* @function shuffle
* @memberof Array
*/
export function shuffle(array, mutateOriginal = true) {
if (mutateOriginal) return array.sort(() => Math.random() - 0.5);
Expand Down
3 changes: 3 additions & 0 deletions src/Array/sortAscending.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,9 @@
* @param {number[]} arr - the array to sort
* @param {boolean} [changeArr=false] - if true will change the original array
* @returns {number[]} A new Array sorted
*
* @function sortAscending
* @memberof Array
*/
export function sortAscending(arr, changeArr = false) {
if (!changeArr) return [...arr].sort((a, b) => a - b);
Expand Down
3 changes: 3 additions & 0 deletions src/Array/sortAscendingObject.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,5 +4,8 @@
* @param {any[]} arr - the array to sort
* @param {string} prop - the property base to sort
* @returns {any[]} - a new sorted array by the given property
*
* @function sortAscendingObject
* @memberof Array
*/
export function sortAscendingObject(arr, prop) { return [...arr].sort((a, b) => a[prop] - b[prop]); }
3 changes: 3 additions & 0 deletions src/Array/sortDescending.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,5 +7,8 @@
*
* @param {number[]} arr - the array to sort
* @returns {number[]} A new Array sorted
*
* @function sortDescending
* @memberof Array
*/
export function sortDescending(arr) { return [...arr].sort((a, b) => b - a); }
3 changes: 3 additions & 0 deletions src/Array/sortDescendingObject.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,5 +4,8 @@
* @param {any[]} arr - the array to sort
* @param {string} prop - the property base to sort
* @returns {any[]} - a new sorted array by the given property
*
* @function sortDescendingObject
* @memberof Array
*/
export function sortDescendingObject(arr, prop) { return [...arr].sort((a, b) => b[prop] - a[prop]); }
25 changes: 25 additions & 0 deletions src/Device/getBrowser.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
/**
* Detects and returns the browser name
*
* Function based in ➜ {@link https://stackoverflow.com/questions/9847580/how-to-detect-safari-chrome-ie-firefox-and-opera-browser}
*
* @example getBrowser(); // Opera
* @example getBrowser(); // Chrome
* @example getBrowser(); // Safari
* @example getBrowser(); // Firefox
* @example getBrowser(); // IE
* @example getBrowser(); // Unknown // Not detect
*
* @returns {String} browser name
*
* @function getBrowser
* @memberof Device
*/
export function getBrowser() {
if ((navigator.userAgent.indexOf("Opera") || navigator.userAgent.indexOf("OPR")) != -1) return "Opera";
else if (navigator.userAgent.indexOf("Chrome") != -1) return "Chrome";
else if (navigator.userAgent.indexOf("Safari") != -1) return "Safari";
else if (navigator.userAgent.indexOf("Firefox") != -1) return "Firefox";
else if (navigator.userAgent.indexOf("MSIE") != -1 || !!document.documentMode == true) return "IE";
else return "Unknown"; // IF IE > 10
}
32 changes: 32 additions & 0 deletions src/Device/getBrowserVersion.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
/**
* Detects and returns the browser version
*
* Function based in ➜ {@link https://stackoverflow.com/questions/5916900/how-can-you-detect-the-version-of-a-browser}
*
* @example getBrowserVersion(); // 106
* @example getBrowserVersion(); // 105
*
* @returns {Number|String} browser version
*
* @function getBrowserVersion
* @memberof Device
*/
export function getBrowserVersion() {
const ua = navigator.userAgent;
let tem = null;
let M = ua.match(/(opera|chrome|safari|firefox|msie|trident(?=\/))\/?\s*(\d+)/i) || [];

if (/trident/i.test(M[1])) {
tem = /\brv[ :]+(\d+)/g.exec(ua) || [];
return "IE " + (tem[1] || "");
}

if (M[1] === "Chrome") {
tem = ua.match(/\b(OPR|Edge)\/(\d+)/);
if (tem != null) return tem.slice(1).join(" ").replace("OPR", "Opera");
}

M = M[2] ? [M[2]] : [navigator.appVersion, "-?"]; // M = M[2] ? [M[1], M[2]] : [navigator.appName, navigator.appVersion, "-?"];
if ((tem = ua.match(/version\/(\d+)/i)) != null) M.splice(1, 1, tem[1]);
return M; // return M.join(" ");
}
36 changes: 36 additions & 0 deletions src/getDate.js → src/Device/getDate.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,9 @@
}
*
* @returns {Object}
*
* @function getDate
* @memberof Device
*/
export function getDate() {
return {
Expand All @@ -43,6 +46,9 @@ export function getDate() {
* getTime() // 1653573577063
*
* @returns {number}
*
* @function getTime
* @memberof Device
*/
export function getTime() { return new Date().getTime(); }

Expand All @@ -55,6 +61,9 @@ export function getTime() { return new Date().getTime(); }
* getMilliseconds() // 952
*
* @returns {number} current milliseconds
*
* @function getMilliseconds
* @memberof Device
*/
export function getMilliseconds() { return new Date().getMilliseconds(); }

Expand All @@ -67,6 +76,9 @@ export function getMilliseconds() { return new Date().getMilliseconds(); }
* getSeconds() // 24
*
* @returns {number} current seconds
*
* @function getSeconds
* @memberof Device
*/
export function getSeconds() { return new Date().getSeconds(); }

Expand All @@ -79,6 +91,9 @@ export function getSeconds() { return new Date().getSeconds(); }
* getMinutes() // 24
*
* @returns {number} current minutes
*
* @function getMinutes
* @memberof Device
*/
export function getMinutes() { return new Date().getMinutes(); }

Expand All @@ -91,6 +106,9 @@ export function getMinutes() { return new Date().getMinutes(); }
* getHours() // 23
*
* @returns {number} current hours
*
* @function getHours
* @memberof Device
*/
export function getHours() { return new Date().getHours(); }

Expand All @@ -104,6 +122,9 @@ export function getHours() { return new Date().getHours(); }
* getDay() // 31
*
* @returns {number} current day
*
* @function getDay
* @memberof Device
*/
export function getDay() { return new Date().getDate(); }

Expand All @@ -117,6 +138,9 @@ export function getDay() { return new Date().getDate(); }
* getWeekDay() // 7
*
* @returns {number} current day of the week
*
* @function getWeekDay
* @memberof Device
*/
export function getWeekDay() { return new Date().getDay() + 1; }

Expand All @@ -128,6 +152,9 @@ export function getWeekDay() { return new Date().getDay() + 1; }
* getWeek() // 50
*
* @returns {number} current week in the year
*
* @function getWeek
* @memberof Device
*/
export function getWeek() {
const currentDate = new Date();
Expand All @@ -148,6 +175,9 @@ export function getWeek() {
* getDay() // 12
*
* @returns {number} current month
*
* @function getMonth
* @memberof Device
*/
export function getMonth() { return new Date().getMonth() + 1; }

Expand All @@ -161,6 +191,9 @@ export function getMonth() { return new Date().getMonth() + 1; }
* getDay() // 2042
*
* @returns {number} current year
*
* @function getYear
* @memberof Device
*/
export function getYear() { return new Date().getFullYear(); }

Expand All @@ -173,5 +206,8 @@ export function getYear() { return new Date().getFullYear(); }
* getDateFormatted() // "25/12/2042"
*
* @returns {string}
*
* @function getDateFormatted
* @memberof Device
*/
export function getDateFormatted() { return getDay() + "/" + getMonth() + "/" + getYear(); }
35 changes: 35 additions & 0 deletions src/Device/getOS.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
/**
* Detects and returns the Operating System name
*
* Function based in ➜ {@link https://stackoverflow.com/questions/38241480/detect-macos-ios-windows-android-and-linux-os-with-js}
*
* @example getOS(); // Mac OS
* @example getOS(); // Windows
* @example getOS(); // iOS
* @example getOS(); // UNIX
* @example getOS(); // Android
* @example getOS(); // Linux
* @example getOS(); // Unknown // Not detect
*
* @returns {String} Operating System name
*
* @function getOS
* @memberof Device
*/
export function getOS() {
const userAgent = window.navigator.userAgent;
const platform = window.navigator.platform;

const macosPlatforms = ["Macintosh", "MacIntel", "MacPPC", "Mac68K"];
const iosPlatforms = ["iPhone", "iPad", "iPod"];
const windowsPlatforms = ["Win32", "Win64", "Windows", "WinCE"];
const unixPlatforms = ["X11"];

if (macosPlatforms.indexOf(platform) !== -1) return "Mac OS";
else if (iosPlatforms.indexOf(platform) !== -1) return "iOS";
else if (windowsPlatforms.indexOf(platform) !== -1) return "Windows";
else if (unixPlatforms.indexOf(userAgent) != -1) return "UNIX";
else if (/Android/.test(userAgent)) return "Android";
else if (!os && /Linux/.test(platform)) return "Linux";
return "Unknown";
}

0 comments on commit 8eb3cbf

Please sign in to comment.