Aleppo
![NPM](https://nodei.co/npm/aleppo.png?downloads=true&downloadRank=true&stars=true)
![license](https://img.shields.io/github/license/mashape/apistatus.svg)
Overview
Aleppo is utility functions written in JavaScript.
Usage
Validation - IS
Function execution - DOO
Time and date
Other
Getting Started
clone the repo:
git clone git@github.com:jimmy02020/aleppo.git
cd aleppo
Using npm:
$ npm install aleppo
Validation - IS
Boolean validation functions. IS-functions can deal with one argument or multiple arguments as it accepts the spread syntax.
$ npm i aleppo.is
isUn (...inputs)
Checks if undefined returns true, otherwise returns false.
Examples
const { isUn } = require('aleppo')
isUn(undefined, a, b, c, d, e)
isUn('hi', 'there', 'I am defined')
isUn('defined', y, undefined)
isNull(...inputs)
Checks if null returns true otherwise returns false.
Examples
const { isNull } = require('aleppo')
isNull(null, null)
isNull(null, "hi")
isNull(undefined, null, 'still', 'false', 'because', 'of undefined and null')
isValid(...inputs)
Checks if not being undefined or null returns true otherwise returns false.
Examples
const { isValid } = require('aleppo')
isValid("welcome", "to", "the", "club")
isValid(null, "hi")
isValid(undefined, null, 'still', 'false', 'because', 'of undefined and null')
isObj(...inputs)
Checks if object returns true otherwise returns false.
Examples
const { isObj } = require('aleppo')
isObj({ boo: 'foo' }, { baz: 'doo' })
isObj(null, { boo: 'foo' }, { baz: 'doo' })
isBool(...inputs)
Checks if Boolean returns true otherwise returns false.
Examples
const { isBool } = require('aleppo')
isBool(true, false)
isBool(1,2,3,4,5,6,7,100)
isNum(...inputs)
Checks if numbers returns true otherwise returns false.
Examples
const { isNum } = require('aleppo')
isNum('hello')
isNum(1,2,3,4,5,6,7,100)
isStr(...inputs)
Checks if string returns true otherwise returns false.
Examples
const { isStr } = require('aleppo')
isStr('hello', 'thanks for' , 'being here')
isStr(1,2,3,4,5,6,7,100)
isFn(...inputs)
Checks if function returns true otherwise returns false.
Examples
const { isFn } = require('aleppo')
isFn(function () { return 'tiny fnc' })
isFn(1,2,3,4,5,6,7,100)
isArr(...inputs)
Checks if arrays returns true otherwise returns false.
Examples
const { isArr } = require('aleppo')
isArr(['yeah'], [1,3], ['sure it is'])
isArr(1,2,3,4,5,6,7,100, 'string')
isZeroLength(...inputs)
Checks if strings zero length returns true otherwise returns false.
Examples
const { isZeroLength } = require('aleppo')
isZeroLength([], '', [])
isZeroLength([1,2,3,4,5,6,7,100], 'string', [], '')
isStrEmpty(...inputs)
Checks if strings are empty returns true otherwise returns false.
Examples
const { isStrEmpty } = require('aleppo')
isStrEmpty('')
isStrEmpty('not empty string')
isArrEmpty(...inputs)
Checks if arrays are empty returns true otherwise returns false.
Examples
const { isArrEmpty } = require('aleppo')
isArrEmpty([])
isArrEmpty(['what do you think?'])
Function execution - DOO
Executes multiple arguments with multiple functions in one call. Returns the results in required form.
It passes each argument to all functions, after it's done with the first argument, it moves to process the second argument, so on so forth.
$ npm i aleppo.doo
doo.iterator([...funcs], [...args])
Returns iterator object of results.
Example : doo.iterator
const { doo } = require('aleppo')
function greetings (name) { return `hello ${name}` }
function bye (name) { return `goodbye ${name}!` }
const result = doo.iterator([greetings, bye], ['Jimmy', 'Catherine'])
result.next()
result.next()
result.next()
result.next()
result.next()
doo.array([...funcs], [...args])
Returns array of results.
Example
const result = doo.array([greetings, bye], ['Jimmy', 'Catherine'])
console.log(result);
[ 'hello Jimmy', 'goodbye Jimmy!', 'hello Catherine', 'goodbye Catherine!' ]
doo.object([...funcs], [...args])
Returns array of objects [{func: 'Function name', result: 'result of the function'}]
Example
const result = doo.object([greetings, bye], ['Jimmy', 'Catherine'])
console.log(result);
[
{ func: 'greetings', result: 'hello Jimmy' },
{ func: 'bye', result: 'goodbye Jimmy!' },
{ func: 'greetings', result: 'hello Catherine' },
{ func: 'bye', result: 'goodbye Catherine!' }
]
Time and date
A set of functions to deal and manipulate with time and date.
Delay
Calculates delay time according to given option then returns it in milliseconds.
$ npm i aleppo.delay
const { delay } = require('aleppo')
Types of options formats are:
year/years/yrs/y/yy/yyy/yyyy
: delay by years.d/dd/day/days
: delay by days.h//hr/hrs/hour/hours
: delay by hours.w/week/weeks
: delay by weeks.mo/mos/month/months/mth/mths
: delay by months.m/min/mins/minute/minutes
: delay by minutes.s/sec/secs/second/seconds
: delay by seconds.ms/mili/milisec/milisecs/milisecond/miliseconds
: delay by milliseconds.
Example
const { delay } = require('aleppo')
delay('1h')
setTimeout(()=>'hello from the other side', delay('10m') + Date.now());
Date:
Deals with time and date, in readable form. It has nine major helper functions to compute time and date.
$ npm i aleppo.date
date.now([, options])
Returns current date or time format according to required option.
Example
const { date } = require('aleppo')
date.now()
date.now('ts')
date.now('hr:day:mo:week')
date.now('year,day,week,month,second')
date.now('year')
date.now('yy')
date.now('mon')
date.now('mth')
date.now('month')
date.now('wk')
date.later(Xoptions[, ts|fullDate])
Returns later date or time according to required option. Later
computes the difference of time accurately, taking into consideration year type and days count in each month.
Xoptions
is String. Combined of number of times with valid options. X is 1 by default.ts
If you want to return value in timestamp, pass 'ts'.fullDate
If you want to return full date object, contains { yr, mo, dy, hr, min, sec}- Complex date format is not allowed.
later
throws an error when options is missing, not String or invalid.
Example
const { date } = require('aleppo')
date.later('10days')
date.later('10d')
date.later('10dd')
date.later('2wk')
date.later('2wk', 'ts')
date.later('2wk', 'fullDate')
date.later('12mo')
date.later('12mth')
date.later('12month')
date.ago(Xoptions[, ts|fullDate])
Returns any required format for time or date happened ago from now. ago
computes the difference of time accurately, taking into consideration year type and days count in each month.
Xoptions
is String. Combined of number of times with valid options. X is 1 by default.ts
If you want to return value in timestamp, pass 'ts'.fullDate
If you want to return full date object, contains { yr, mo, dy, hr, min, sec}- Complex date format is not allowed.
ago
throws an error when options is missing, not String or invalid.
Example
const { date } = require('aleppo')
date.ago('hour')
date.ago('hr24')
date.ago('hr12')
date.ago('2hr12')
date.ago('2hr12', 'ts')
date.readTs(timestamp)
Returns readable timestamp object. Contains yr, mo, dy, hr, min, sec.
timestamp
can be Number or String.readTs
throws an error when timestamp is missing or invalid.
Example
const { date } = require('aleppo')
date.readTs(1503247105430)
{ yr: 2017, mo: 5, dy: 23, hr: 19, min: 15, sec: 45 }
date.isLeap([, year])
Returns Boolean. True if the year is leap. False for otherwise.
- Default value is current year.
isLeap
throws an error when year is not valid number or valid value.
Example
const { date } = require('aleppo')
date.isLeap(2019)
date.isLeap(2000)
date.isLeap()
date.isCommon([, year])
Returns Boolean. True if the year is common. False for otherwise.
- Default value is current year.
isCommon
throws an error when year is not valid number or valid value.
Example
const { date } = require('aleppo')
date.isCommon(2028)
date.isCommon(2018)
date.isCommon()
date.yearType([, year])
Returns String of year type common or leap.
year
is Number- Default value is current year.
yearType
throws an error when year is not valid number or valid value.
Example
const { date } = require('aleppo')
const date = getters.date
date.yearType(2000)
date.yearType(1999)
date.yearType(2028)
date.yearType()
date.daysCountInMonth([, monthNum] [, year])
Returns number of days in required month. Taking into consideration whether the year is common, or leap to return the right number of days for February.
monthNum
and year
are Numbers- Default value for
monthNum
is current month number, year
is current year. daysCountInMonth
throws an error if:
- Month number is not correct.
- One of the input date is not valid number.
Example
const { date } = require('aleppo')
date.daysCountInMonth(2, 2018)
date.daysCountInMonth(2, 2016)
date.daysCountInMonth(1, 2017)
date.daysCountInMonth()
date.countBtw(type[, [, from|timestamp] [, to|timestamp ]] [-i])
date.countBtw(type)
date.countBtw(type, to)
date.countBtw(type, from, to, i)
Computes the difference between two dates and returns objects of results.
type
is type of request, all/year/month/week/day. All relevant options are valid.from
String or Number.
- Default value is 1/1/currentYear.
- String for date format request should be "dd/mm/yyyy".
- Number will be explained according to request type.
- If your request type is
year
number will be converted to 1/1/year. month
to 1/month/currentYear. day
to day/1/currentYear.
to
String or Number. Same as from
with major difference. to
converts number to current date instead of beginning of the year.
- Default value is currentDay/currentMonth/currentYear.
- If your request type is
year
number will be converted to: currentDay/currentMonth/year. months
to currentDay/month/currentYear. day
to day/currentMonth/currentYear.
i
A flag constiable, false by default. If you want to include the last day in the result, pass 'i' to include it.- returns object of request type and remainder of calculations if there is any.
Note:
- Both
from
and to
accepts timestamp as numbers. - One date argument is considered as
to
. - Date format can use one of the following separators.
countBtw
throws an error if:
- No type is provided.
- One of the input date is not valid.
- Type of request is not valid or missing.
Example
const { date } = require('aleppo')
date.countBtw('yrs')
{ years: 0, months: 5, days: 15 }
date.countBtw('yrs', 2017)
{ years: 0, months: 5, days: 15 }
date.countBtw('yrs', 2017, 2050, 'i')
{ years: 33, months: 5, days: 16 }
date.countBtw('yrs', '13/5/2089', '1/1/2010')
{ years: 79, months: 4, days: 12 }
date.countBtw('yrs', '1.1.1990', 1496337691025)
{ years: 27, months: 5, days: 0 }
date.countBtw('mos')
{ months: 5, days: 15 }
date.countBtw('mos', 10)
{ months: 9, days: 15 }
date.countBtw('mos', 10, 12, 'i')
{ months: 2, days: 16 }
date.countBtw('mos', '13/5/2089', '1/1/2010')
{ months: 952, days: 12 }
date.countBtw('day')
{ days: 166 }
date.countBtw('day', 10)
{ days: 174 }
date.countBtw('d', '13/5/2089', '1/1/2010')
{ days: 28987 }
date.countBtw('weeks', '1/1/2010', '1/1/2011')
{ weeks: 52, days: 1 }
date.countBtw('all', '1/1/2010', '18/9/2031')
{
inDays: 7930,
inWeeks: { weeks: 1132, days: 6 },
inMonths: { months: 260, days: 17 },
inYears: { years: 21, months: 8, days: 17 }
}
Available options format:
year/years/yrs/yyyy
: full year/ four digits.y/yy/yyy
: two digits year.mo/mon/mos/mons
: month number.mth/mths
: month short name.month/months
: month full name.w/wk/week/weeks
: week number.d
: day number in the week.dd
: day short name.day/days
: day full name.h/hr/hrs/hour/hours/h24/hr24/hrs24/hour24/hours24
: 24-hour form.h12/hr12/hrs12/hour12/hours12
: 12-hour form.m/min/mins/minute/minutes
: minutes.s/sec/secs/second/seconds
: seconds.ms/mili/milisec/milisecs/milisecond/miliseconds
: milliseconds.ts/timestamp
: timestamp. (available only for now function)local
: local date and time. (available only for now function)local date/localDate
: local date.(available only for now function)local time/localTime
: local time. (available only for now function)utc
: UTC time. (available only for now function)
Types of valid separators are:
-
or /
or .
or :
or ,
or <space>
Tests
$ npm test
License
This project is licensed under the MIT License