jshelper
v1.0.22
Published
Some helper functions
Downloads
9
Readme
JsHelpers
Just some JsHelpers I made for myself.
npm install jshelper
Example
ES6
import JsHelper = 'jshelper';
import Objects = 'jshelper/Objects';
import { find } = 'jshelper/Objects';
var data = {
person: {
name: 'Ray Davis',
age: 23,
},
job: 'idk',
};
const _jsHelper = JsHelper.Objects.find(data, 'person.name');
const _objects = Objects.find(data, 'person.name');
const _valueFromObject = find(data, 'person.name');
console.log(`JsHelper Output: ${_jsHelper}`);
console.log(`Objects Output: ${_objects}`);
console.log(`valueFromObject Output: ${_valueFromObject}`);
Non ES6
const JsHelper = require('jshelper').default;
API
JsHelper
JsHelper imports everything
Objects
find
This function search the item using a nexted string path
Parameters
item
Object The object you want to search inpropertyName
String The path to the value seperated by dots (.). (optional, default''
)
Returns (any | undefined) The found value or nothing
Arrays
removeDupilcates
This function will remove all dupilcates from a array
Parameters
array
Array The array to remove dupilcates from.
Returns Array The new array
sortObjects
This function will sort an array of objects no matter the depth
Parameters
array
Array The array to sortpropertyName
String The path to the value seperated by dots (.) (optional, default''
)config
Object An object containing additional setting for the function (optional, default{}
)
Returns Array The sorted array
filterObjects
This function will folter an array of objects no matter the depth
Parameters
array
Array The array to filtermatch
string What you want to find (Currently only support string) Need support for number boolean and custom match functionpropertyName
string The path to the value seperate by dotes (.) (optional, default''
)config
Object An object containing additional setting for the function (optional, default{}
)config.caseSen
string The type of case sensitive to use when sorting (optional, default'lower'
)
Returns Array The flitered array
find
This function search the item using a nexted string path
Parameters
item
Object The object you want to search inpropertyName
String The path to the value seperated by dots (.). (optional, default''
)
Returns (any | undefined) The found value or nothing
Dates
isValid
This function returns true if date is an instanceof Date
Parameters
date
Date any object that is an instanceof Date
Returns boolean if the date is an instanceof Date
daysInMonth
This function returns the number of days in a month
Parameters
date
Date any object that is an instanceof Date (optional, defaultnew Date()
)
Returns (number | undefined) The number of days in month or undefined if date is no valid
getMonthBounds
This function returns the start and end dates of the month
Parameters
date
Date any object that is an instanceof Date (optional, defaultnew Date()
)
Returns (Object | undefined) A object containing the start and end dates or nothing
getWeekBounds
This function returns the start and end dates of the week
Parameters
date
Date any object that is an instanceof Date (optional, defaultnew Date()
)
Returns (Object | undefined) A object containing the start and end dates or nothing
getWeekBoundsCentered
This function returns the start and end dates of the week centered around the date
Parameters
date
Date any object that is an instanceof Date (optional, defaultnew Date()
)
Returns (Object | undefined) A object containing the start and end dates or nothing
getDayBounds
This function returns the start and end dates of the day
Parameters
date
Date any object that is an instanceof Date (optional, defaultnew Date()
)
Returns (Object | undefined) A object containing the start and end dates or nothing
addToDate
This function returns a new date with the additions
Parameters
config
Object An object containing the fields and how much to addconfig.years
number The number of years to add (optional, default0
)config.months
number The number of months to add (optional, default0
)config.days
number The number of days to add (optional, default0
)config.hours
number The number of hours to add (optional, default0
)config.minutes
number The number of minutes to add (optional, default0
)config.seconds
number The number of seconds to add (optional, default0
)config.milliseconds
number The number of milliseconds to add (optional, default0
)
date
Date any object that is an instanceof Date (optional, defaultnew Date()
)
Returns (Date | undefined) A Date with the additions or nothing
addYears
This function returns a new Date with the added years
Parameters
years
(optional, default0
)date
Date any object that is an instanceof Date (optional, defaultnew Date()
)
Returns (Date | undefined) A new Date with the addition or nothing
addMonths
This function returns a new Date with the added months
Parameters
months
(optional, default0
)date
Date any object that is an instanceof Date (optional, defaultnew Date()
)
Returns (Date | undefined) A new Date with the addition or nothing
addWeeks
This function returns a new Date with the added weeks
Parameters
weeks
(optional, default0
)date
Date any object that is an instanceof Date (optional, defaultnew Date()
)
Returns (Date | undefined) A new Date with the addition or nothing
addDays
This function returns a new Date with the added days
Parameters
days
(optional, default0
)date
Date any object that is an instanceof Date (optional, defaultnew Date()
)
Returns (Date | undefined) A new Date with the addition or nothing
addHours
This function returns a new Date with the added hours
Parameters
hours
(optional, default0
)date
Date any object that is an instanceof Date (optional, defaultnew Date()
)
Returns (Date | undefined) A new Date with the addition or nothing
addMinutes
This function returns a new Date with the added minutes
Parameters
minutes
(optional, default0
)date
Date any object that is an instanceof Date (optional, defaultnew Date()
)
Returns (Date | undefined) A new Date with the addition or nothing
addSeconds
This function returns a new Date with the added seconds
Parameters
seconds
(optional, default0
)date
Date any object that is an instanceof Date (optional, defaultnew Date()
)
Returns (Date | undefined) A new Date with the addition or nothing
addMilliseconds
This function returns a new Date with the added milliseconds
Parameters
milliseconds
(optional, default0
)date
Date any object that is an instanceof Date (optional, defaultnew Date()
)
Returns (Date | undefined) A new Date with the addition or nothing