ConditionalPeriod
ConditionalPeriod
is a js class allowing to store 2 numerical/Duration
intervals resulting in a Duration
.
It is, actually, a requirement for a project I'm working on. Typically, it concerns the working contracts. Here is an example using integers as conditions:
- If the category of the employee is between 1 and 5, the prior notice is 1 month.
- If the category of the employee is between 6 and 7, the prior notice is 2 months.
- If the category of the employee is between 8 and 12, the prior notice is 3 months.
Here is another example using date intervals as conditions:
- If the fixed term contract lasts from 0 to 6 months, then the trial period is 15 days.
- Above 6 months, the trial period is 1 month.
Expressing these interval with conditions using ConditionalPeriod
is super duper easy and can be expressed in 2 ways: Using 4 arguments on the constructor, or using a string syntax.
Installation
You can use composer: npm install conditional-period:^1.0
Or download the repo and add the files (in /src
) to your project.
Usage
Using 4 arguments on the constructor, aka "classic instantiation"
const Duration = ;const ConditionalCollection ConditionalPeriod ConditionalType } = ;...let prior_notices = ConditionalTypeCATEGORY 1 5 Duration ConditionalType::CATEGORY 6 7 Duration ConditionalType::CATEGORY 8 12 Duration ; prior_notices = ConditionalCollection; let trial_periods = ConditionalType::DURATION Duration Duration Duration ConditionalType::DURATION Duration Duration // Equivalent to +∞ Duration ; trial_periods = ConditionalCollection;
Using the short string format, aka "badass mode"
const ConditionalCollection ConditionalPeriod } = ;...let prior_notices = 'C1-5P1M' 'C6-7P2M' 'C8-12P3M'; // ORprior_notices = ;prior_notices;prior_notices;prior_notices; let trial_periods = 'DP0DP6MP15D' 'DP6MP99YP1M'; // ORtrial_periods = ConditionalCollection;
Miscellaneous
You may of may not have noticed it, but every Duration
argument can be replaced by either its ISO8601
duration spec (the same way you can instanciate a Duration
.
So, here are the 2 same ways to input a Duration
using ConditionalPeriod
constructor:
Duration.fromISO('P1Y2M3D');
'P1Y2M3D'
And there are 4 ways to create a ConditionalCollection
:
ConditionalCollection.create(new ConditionalPeriod(…))
, which instanciates it and sets its first value (); // Or ConditionalPeriod string formnew ConditionalCollection
, then push to it:c.push(new ConditionalPeriod(…)); // or ConditionalPeriod string form
ConditionalCollection.parse(…)
, which takes its owntoString()
formConditionalCollection.fromArray(…)
, which takes an array ofConditionalPeriod
or its string form
Need help?
Open an issue.
Now have fun.