Added get function
Bumped to version 2.0.2
This commit is contained in:
parent
26a92aa2d6
commit
b0e843fd3e
@ -1,6 +1,6 @@
|
|||||||
{
|
{
|
||||||
"name": "@rakh/utils",
|
"name": "@rakh/utils",
|
||||||
"version": "2.0.1",
|
"version": "2.0.2",
|
||||||
"main": "dist/commonjs/index.js",
|
"main": "dist/commonjs/index.js",
|
||||||
"module": "dist/es/index.js",
|
"module": "dist/es/index.js",
|
||||||
"jsnext:main": "dist/es/index.js",
|
"jsnext:main": "dist/es/index.js",
|
||||||
|
103
src/utils.js
103
src/utils.js
@ -6,37 +6,23 @@
|
|||||||
*/
|
*/
|
||||||
const partOfDay = (timeString, today) => {
|
const partOfDay = (timeString, today) => {
|
||||||
console.log(new Date());
|
console.log(new Date());
|
||||||
if (timeString === undefined || timeString === null)
|
if (timeString === undefined || timeString === null) timeString = new Date().getHours().toString();
|
||||||
timeString = (new Date()).getHours().toString();
|
|
||||||
|
|
||||||
if (today === undefined)
|
if (today === undefined) today = false;
|
||||||
today = false;
|
|
||||||
|
|
||||||
const hours = timeString.substring(0, 2);
|
const hours = timeString.substring(0, 2);
|
||||||
let dayBit;
|
let dayBit;
|
||||||
|
|
||||||
console.log('Hours', hours);
|
console.log('Hours', hours);
|
||||||
|
|
||||||
if (hours >= 0 && hours < 4)
|
if (hours >= 0 && hours < 4) dayBit = 'Late Night';
|
||||||
dayBit = 'Late Night';
|
else if (hours >= 4 && hours < 7) dayBit = 'Early Morning';
|
||||||
|
else if (hours >= 7 && hours < 12) dayBit = 'Morning';
|
||||||
|
else if (hours >= 12 && hours < 17) dayBit = 'Afternoon';
|
||||||
|
else if (hours < 21) dayBit = 'Evening';
|
||||||
|
else dayBit = 'Night';
|
||||||
|
|
||||||
else if (hours >= 4 && hours < 7)
|
if (today) dayBit = dayBit === 'night' ? 'tonight' : `this ${dayBit}`;
|
||||||
dayBit = 'Early Morning';
|
|
||||||
|
|
||||||
else if (hours >= 7 && hours < 12)
|
|
||||||
dayBit = 'Morning';
|
|
||||||
|
|
||||||
else if (hours >= 12 && hours < 17)
|
|
||||||
dayBit = 'Afternoon';
|
|
||||||
|
|
||||||
else if (hours < 21)
|
|
||||||
dayBit = 'Evening';
|
|
||||||
|
|
||||||
else
|
|
||||||
dayBit = 'Night';
|
|
||||||
|
|
||||||
if (today)
|
|
||||||
dayBit = dayBit === 'night' ? 'tonight' : `this ${dayBit}`;
|
|
||||||
|
|
||||||
console.log('partOfDay', dayBit);
|
console.log('partOfDay', dayBit);
|
||||||
|
|
||||||
@ -50,7 +36,7 @@ const partOfDay = (timeString, today) => {
|
|||||||
*/
|
*/
|
||||||
const toHour = (extra = 0) => {
|
const toHour = (extra = 0) => {
|
||||||
const now = new Date();
|
const now = new Date();
|
||||||
return (3600000 - (now.getTime() % 3600000)) + extra;
|
return 3600000 - (now.getTime() % 3600000) + extra;
|
||||||
};
|
};
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@ -75,10 +61,10 @@ const getWeek = function (d) {
|
|||||||
var firstThursday = target.valueOf();
|
var firstThursday = target.valueOf();
|
||||||
target.setMonth(0, 1);
|
target.setMonth(0, 1);
|
||||||
if (target.getDay() != 4) {
|
if (target.getDay() != 4) {
|
||||||
target.setMonth(0, 1 + ((4 - target.getDay()) + 7) % 7);
|
target.setMonth(0, 1 + ((4 - target.getDay() + 7) % 7));
|
||||||
}
|
}
|
||||||
return 1 + Math.ceil((firstThursday - target) / 604800000);
|
return 1 + Math.ceil((firstThursday - target) / 604800000);
|
||||||
}
|
};
|
||||||
|
|
||||||
/**
|
/**
|
||||||
*
|
*
|
||||||
@ -91,16 +77,12 @@ const getWeek = function (d) {
|
|||||||
const distance = (lat1, lon1, lat2, lon2) => {
|
const distance = (lat1, lon1, lat2, lon2) => {
|
||||||
const p = 0.017453292519943295; // Math.PI / 180
|
const p = 0.017453292519943295; // Math.PI / 180
|
||||||
const c = Math.cos;
|
const c = Math.cos;
|
||||||
const a = 0.5 - c((lat2 - lat1) * p) / 2 +
|
const a = 0.5 - c((lat2 - lat1) * p) / 2 + (c(lat1 * p) * c(lat2 * p) * (1 - c((lon2 - lon1) * p))) / 2;
|
||||||
c(lat1 * p) * c(lat2 * p) *
|
|
||||||
(1 - c((lon2 - lon1) * p)) / 2;
|
|
||||||
|
|
||||||
return 12742 * Math.asin(Math.sqrt(a)); // 2 * R; R = 6371 km
|
return 12742 * Math.asin(Math.sqrt(a)); // 2 * R; R = 6371 km
|
||||||
};
|
};
|
||||||
|
|
||||||
function splitURL(url) {
|
function splitURL(url) {}
|
||||||
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
*
|
*
|
||||||
@ -109,8 +91,7 @@ function splitURL(url) {
|
|||||||
* @param suffix
|
* @param suffix
|
||||||
* @returns {string}
|
* @returns {string}
|
||||||
*/
|
*/
|
||||||
const maybePluralize = (count, noun, suffix = 's') =>
|
const maybePluralize = (count, noun, suffix = 's') => `${count} ${noun}${count !== 1 ? suffix : ''}`;
|
||||||
`${count} ${noun}${count !== 1 ? suffix : ''}`;
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
*
|
*
|
||||||
@ -122,7 +103,8 @@ const maybePluralize = (count, noun, suffix = 's') =>
|
|||||||
const debounce = function (fn, time) {
|
const debounce = function (fn, time) {
|
||||||
let timeout;
|
let timeout;
|
||||||
|
|
||||||
return function (...args) { // <-- not an arrow function
|
return function (...args) {
|
||||||
|
// <-- not an arrow function
|
||||||
const functionCall = () => fn.apply(this, args);
|
const functionCall = () => fn.apply(this, args);
|
||||||
|
|
||||||
clearTimeout(timeout);
|
clearTimeout(timeout);
|
||||||
@ -176,7 +158,7 @@ const once = function (func) {
|
|||||||
* @param obj
|
* @param obj
|
||||||
* @returns {boolean|boolean}
|
* @returns {boolean|boolean}
|
||||||
*/
|
*/
|
||||||
const isEmpty = obj => [Object, Array].includes((obj || {}).constructor) && !Object.entries((obj || {})).length;
|
const isEmpty = (obj) => [Object, Array].includes((obj || {}).constructor) && !Object.entries(obj || {}).length;
|
||||||
|
|
||||||
const hasOwnProperty = Object.prototype.hasOwnProperty;
|
const hasOwnProperty = Object.prototype.hasOwnProperty;
|
||||||
|
|
||||||
@ -196,11 +178,12 @@ const hasOwn = (obj, prop) => hasOwnProperty.call(obj, prop);
|
|||||||
* @returns {*}
|
* @returns {*}
|
||||||
*/
|
*/
|
||||||
const get = (obj, path, defaultValue = undefined) => {
|
const get = (obj, path, defaultValue = undefined) => {
|
||||||
const travel = regexp =>
|
const travel = (regexp) => {
|
||||||
String.prototype.split
|
return String.prototype.split
|
||||||
.call(path, regexp)
|
.call(path, regexp)
|
||||||
.filter(Boolean)
|
.filter(Boolean)
|
||||||
.reduce((res, key) => (res !== null && res !== undefined ? res[key] : res), obj);
|
.reduce((res, key) => (res !== null && res !== undefined ? res[key] : res), obj);
|
||||||
|
};
|
||||||
const result = travel(/[,[\]]+?/) || travel(/[,[\].]+?/);
|
const result = travel(/[,[\]]+?/) || travel(/[,[\].]+?/);
|
||||||
|
|
||||||
return result === undefined || result === obj ? defaultValue : result;
|
return result === undefined || result === obj ? defaultValue : result;
|
||||||
@ -208,22 +191,20 @@ const get = (obj, path, defaultValue = undefined) => {
|
|||||||
|
|
||||||
const arrayFromObj = (jsonObj, wantedFields) => {
|
const arrayFromObj = (jsonObj, wantedFields) => {
|
||||||
return wantedFields.map((key) => {
|
return wantedFields.map((key) => {
|
||||||
if (jsonObj.hasOwnProperty(key))
|
if (jsonObj.hasOwnProperty(key)) return jsonObj[key];
|
||||||
return jsonObj[key];
|
|
||||||
});
|
});
|
||||||
};
|
};
|
||||||
|
|
||||||
const extractFromObj = (jsonObj, wantedFields) => {
|
const extractFromObj = (jsonObj, wantedFields) => {
|
||||||
return Object.keys(jsonObj).reduce((obj, key) => {
|
return Object.keys(jsonObj).reduce((obj, key) => {
|
||||||
if (wantedFields.includes(key))
|
if (wantedFields.includes(key)) obj[key] = jsonObj[key];
|
||||||
obj[key] = jsonObj[key];
|
|
||||||
|
|
||||||
return obj;
|
return obj;
|
||||||
}, {});
|
}, {});
|
||||||
};
|
};
|
||||||
|
|
||||||
const objectMatcher = (obj, matcher) => {
|
const objectMatcher = (obj, matcher) => {
|
||||||
if (typeof(obj) !== 'object' || obj === null) return false;
|
if (typeof obj !== 'object' || obj === null) return false;
|
||||||
|
|
||||||
let count = Object.keys(obj).length;
|
let count = Object.keys(obj).length;
|
||||||
const keys = Object.keys(obj);
|
const keys = Object.keys(obj);
|
||||||
@ -231,30 +212,28 @@ const objectMatcher = (obj, matcher) => {
|
|||||||
if (count !== matcher.length) return false;
|
if (count !== matcher.length) return false;
|
||||||
|
|
||||||
matcher.forEach((item) => {
|
matcher.forEach((item) => {
|
||||||
if (keys.indexOf(item) !== -1)
|
if (keys.indexOf(item) !== -1) count--;
|
||||||
count--;
|
|
||||||
});
|
});
|
||||||
|
|
||||||
return (count === 0);
|
return count === 0;
|
||||||
};
|
};
|
||||||
|
|
||||||
exports = {
|
exports = {
|
||||||
'partOfDay': partOfDay,
|
partOfDay: partOfDay,
|
||||||
'toHour': toHour,
|
toHour: toHour,
|
||||||
'hourFloor': hourFloor,
|
hourFloor: hourFloor,
|
||||||
'getWeek': getWeek,
|
getWeek: getWeek,
|
||||||
'distance': distance,
|
distance: distance,
|
||||||
'maybePluralize': maybePluralize,
|
maybePluralize: maybePluralize,
|
||||||
'debounce': debounce,
|
debounce: debounce,
|
||||||
'throttle': throttle,
|
throttle: throttle,
|
||||||
'once': once,
|
once: once,
|
||||||
'isEmpty': isEmpty,
|
isEmpty: isEmpty,
|
||||||
'hasOwn': hasOwn,
|
hasOwn: hasOwn,
|
||||||
'get': get,
|
get: get,
|
||||||
'arrayFromObj' : arrayFromObj,
|
arrayFromObj: arrayFromObj,
|
||||||
'extractFromObj' : extractFromObj,
|
extractFromObj: extractFromObj,
|
||||||
'objectMatcher' : objectMatcher
|
objectMatcher: objectMatcher
|
||||||
|
|
||||||
};
|
};
|
||||||
|
|
||||||
// module.exports = { partOfDay, toHour, hourFloor, distance, maybePluralize, debounce, throttle, once, isEmpty, hasOwn, get, Logger, LocalStorage };
|
// module.exports = { partOfDay, toHour, hourFloor, distance, maybePluralize, debounce, throttle, once, isEmpty, hasOwn, get, Logger, LocalStorage };
|
||||||
|
35
ts-src/get.test.ts
Normal file
35
ts-src/get.test.ts
Normal file
@ -0,0 +1,35 @@
|
|||||||
|
import { get } from './get';
|
||||||
|
|
||||||
|
test('Get something from the object', function () {
|
||||||
|
const o = { a: 1, b: 2 };
|
||||||
|
|
||||||
|
expect(get(o, 'b')).toEqual(2);
|
||||||
|
});
|
||||||
|
|
||||||
|
test('Get something from a complex object', function () {
|
||||||
|
const o = {
|
||||||
|
a: 1,
|
||||||
|
b: {
|
||||||
|
c: 3,
|
||||||
|
d: {
|
||||||
|
e: 5
|
||||||
|
}
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
expect(get(o, 'b')).toEqual({ c: 3, d: { e: 5 } });
|
||||||
|
});
|
||||||
|
|
||||||
|
test('Get a deep item from a complex object', function () {
|
||||||
|
const o = {
|
||||||
|
a: 1,
|
||||||
|
b: {
|
||||||
|
c: 3,
|
||||||
|
d: {
|
||||||
|
e: 5
|
||||||
|
}
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
expect(get(o, 'b.d.e')).toEqual(5);
|
||||||
|
});
|
24
ts-src/get.ts
Normal file
24
ts-src/get.ts
Normal file
@ -0,0 +1,24 @@
|
|||||||
|
/**
|
||||||
|
*
|
||||||
|
* @param obj
|
||||||
|
* @param path
|
||||||
|
* @param defaultValue
|
||||||
|
*/
|
||||||
|
export function get(obj: Object, path: string, defaultValue: any = undefined): any {
|
||||||
|
const travel = (regexp: any) => {
|
||||||
|
return String.prototype.split
|
||||||
|
.call(path, regexp)
|
||||||
|
.filter(Boolean)
|
||||||
|
.reduce((res, key) => {
|
||||||
|
if (res !== null && res !== undefined && res.hasOwnProperty(key)) {
|
||||||
|
const { value }: any = Object.getOwnPropertyDescriptor(res, key);
|
||||||
|
return value;
|
||||||
|
} else {
|
||||||
|
return res;
|
||||||
|
}
|
||||||
|
}, obj);
|
||||||
|
};
|
||||||
|
const result = travel(/[,[\].]+?/);
|
||||||
|
|
||||||
|
return result === undefined || result === obj ? defaultValue : result;
|
||||||
|
}
|
Loading…
Reference in New Issue
Block a user