0.0.7 • Published 5 years ago

@umalqura/core v0.0.7

Weekly downloads
138
License
MIT
Repository
github
Last release
5 years ago

umalqura

Zero dependency Hijri calendar based on Um AlQura

npm package travis Coverage Status

Getting Started

Run the following npm or yran command:

npm i @umalqura/core
-or-
yarn add @umalqura/core

You can also use the library directly in the browser by adding the following to your html:

<script src="https://unpkg.com/@umalqura/core@0.0.7/dist/umalqura.min.js"></script>

Usage

NodeJS

import uq from '@umalqura/core';

// Initializes a new umalqura instance using current date and time.
const now = uq();
// Do more stuff
// ...

Browser

A global var named umalqura will be available.

// Initializes a new umalqura instance using current date and time.
var now = umalqura();
// Do more stuff
// ...

API

Construction
// Initializes using current date and time.
umalqura();
// Initializes using provided date and time.
umalqura(date: Date);
// Initializes using provided Hijri year, month, day and the optionally provided time.
umalqura(hy: number, hm: number, hd: number, hour?: number, minute?: number, second?: number, millisecond?: number);

The umalqura(...) function returns an initialized umalqura.UmAlQura object which exposes the following properties and methods:

Properties
NameDescription
dateReturns the Date object.
hyReturns the Hijri year.
hmReturns the Hijri month.
hdReturns the Hijri day of month.
dayOfYearReturns the Hijri day of year.
dayOfWeekReturns the day of week.
weekOfYearReturns the Hijri week of year.
daysInYearReturns the number of days in current Hijri year.
daysInMonthReturns the number of days in current Hijri month and year.
isLeapYearReturns true if leap year, false otherwise.
monthArrayReturns the dates of month arranged in a 2D array.

Examples:

import uq from '@umalqura/core';

const d = uq(new Date(2019, 6, 3));  // July 3rd 2019
console.log(d.hy, d.hm, d.hd);       // Outputs 1440, 10, 30

// -or

const d = uq(1440, 10, 30);
console.log(d.date);                 // Outputs Wed Jul 03 2019 00:00:00 TZ...
Methods

Note that all methods are immutably.

NameArgumentsDescription
addv:numberu:unitMsReturns a new UmAlQura object adding the specified number of Hijri units to the current instance.
subtractv:numberu:unitMsReturns a new UmAlQura object subtracting the specified number of Hijri units to the current instance.
startOfu:unitReturns a new UmAlQura object starting at the specified Hijri unit of time.
endOfu:unitReturns a new UmAlQura object ending at the specified Hijri unit of time.
isBeforecmp:UmAlQura\|Dateunit?:unitMsReturns whether current instance is before cmp. Check can be pinned down to unitMs which defaults to milliseconds.
isAftercmp:UmAlQura\|Dateunit?:unitMsReturns whether current instance is after cmp. Check can be pinned down to unitMs which defaults to milliseconds.
isSamecmp:UmAlQura\|Dateunit?:unitMsReturns whether current instance is same as cmp. Check can be pinned down to unitMs which defaults to milliseconds.
isSameOrBeforecmp:UmAlQura\|Dateunit?:unitMsReturns whether current instance is same as or before cmp. Check can be pinned down to unitMs which defaults to milliseconds.
isSameOrAftercmp:UmAlQura\|Dateunit?:unitMsReturns whether current instance is same as or after cmp. Check can be pinned down to unitMs which defaults to milliseconds.
isBetweenfrom:UmAlQura\|Dateto:UmAlQura\|DatefromIncl?:booleantoIncl?:booleanunit?:unitMsReturns whether current instance is between from and to. Check can be pinned down to unitMs which defaults to milliseconds. By, default the match is exclusive of both ends. This can be controlled via fromIncl and toIncl.
formatmask:stringlocale?:stringReturns a formatted string of the Hijri date and time using the specified mask and optionally a locale.

unitMs can be any of the following strings: year, month, week, day, hour, minute, second, millisecond.

unit can be any of the following string: year, month, week, day, hour, minute, second.

Examples:

import uq from '@umalqura/core';

const d = uq(new Date(2019, 6, 3));
// Add 5 Hijri months
const after5HijriMonths = d.add(5, 'month');

// ٣٠/١٠/١٤٤٠
d.format('dd/MM/yyyy', 'ar');
// 30/03/1441
after5HijriMonths.format('dd/MM/yyyy');
// True
after5HijriMonths.isAfter(d);
// Shawwāl 1, 1440
d.startOf('month').format('longDate');

const endOf1440H = d.endOf('year');
// الجمعة، ٢٩ ذو الحجة، ١٤٤٠
endOf1440H.format('fullDate', 'ar');
// Fri Aug 30 2019 23:59:59 TZ...
endOf1440H.date;

The following properties and functions exist directly on the export umalqura object:

NameDescription
VERSIONReturns the library version.
$Gives access to static methods.
minReturns the minimum supported Hijri date.
maxReturns the maximum supported Hijri date.
locale(locale?: string)Gets or sets the local to be used globally.
rtl()Returns whether current locale supports RTL.
times()Returns the times names array according to currently set global locale.
days()Returns the days names array according to currently set global locale.
daysShort()Returns the short days names array according to currently set global locale.
months()Returns the Hijri months names array according to currently set global locale.
monthsShort()Returns the short Hijri month names array according to currently set global locale.
localizeNum(n: number)Returns a localized string representing the specified number according to currently set global locale.

Examples:

import uq from '@umalqura/core';

// Set global locale to Arabic so that we no longer need to specify a locale when calling format() function.
uq.locale('ar');
// Returns true
uq.rtl();
// Returns ['محرم', 'صفر', 'ربيع الأول', 'ربيع الثاني', 'جمادى الأولى', 'جمادى الآخرة', 'رجب', 'شعبان', 'رمضان', 'شوال', 'ذو القعدة', 'ذو الحجة']
uq.months();
Pre-defined Formatting Masks
NameFormat
defaultddd dd MMM yyyy HH:mm:ss for arddd MMM dd yyyy HH:mm:ss for en
shortDateyy/M/d for arM/d/yy for en
mediumDated MMM, yyyy for arMMM d, yyyy for en
longDated MMMM, yyyy for arMMMM d, yyyy for en
fullDatedddd, d MMMM, yyyy for ardddd, MMMM d, yyyy for en
shortTimeh:mm TT
mediumTimeh:mm:ss TT
longTimeh:mm:ss.l TT

Sample

An HTML-based sample is available in ./sample/browser.html locally or online @ https://umalqura.github.io/.