Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
45 changes: 45 additions & 0 deletions src/drafting/Duration/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
/*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

type Duration = {
amount: number;
unit: string;
};

/**
* Type guard to check if a value is a valid Duration
*/
function isDuration(value: unknown): value is Duration {
return (
value != null &&
typeof value === 'object' &&
'amount' in value &&
'unit' in value &&
typeof (value as Duration).amount === 'number' &&
typeof (value as Duration).unit === 'string'
);
}

/**
* Creates a drafter for Duration/Period types
* @param {unknown} value the duration or period (validated at runtime)
* @returns {string} the text (e.g., "2 days")
*/
export default function durationDrafter(value: unknown): string {
if (!isDuration(value)) {
return '0 unknown';
}

return `${value.amount} ${value.unit}`;
}
3 changes: 3 additions & 0 deletions src/drafting/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ import booleanDrafter from './Boolean';
import dateTimeDrafter from './DateTime';
import doubleDrafter from './Double';
import integerDrafter from './Integer';
import durationDrafter from './Duration';
import longDrafter from './Long';
import monetaryAmountDrafter from './MonetaryAmount';
import { DraftFormat } from './DraftFormat';
Expand All @@ -31,6 +32,8 @@ export function getDrafter(typeName: string) : ((value:any, format?:DraftFormat)
case 'Integer': return integerDrafter;
case 'Long': return longDrafter;
case 'org.accordproject.money@0.3.0.MonetaryAmount': return monetaryAmountDrafter;
case 'org.accordproject.time@0.3.0.Duration': return durationDrafter;
case 'org.accordproject.time@0.3.0.Period': return durationDrafter;
case 'String': return stringDrafter;
default: return null;
}
Expand Down
37 changes: 37 additions & 0 deletions test/DurationDrafter.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
/*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

import durationDrafter from '../src/drafting/Duration';

describe('Duration Drafter', () => {
test('should format Duration with days', () => {
const duration = { amount: 2, unit: 'days' };
expect(durationDrafter(duration)).toBe('2 days');
});

test('should format Duration with hours', () => {
const duration = { amount: 24, unit: 'hours' };
expect(durationDrafter(duration)).toBe('24 hours');
});

test('should format Period with months', () => {
const period = { amount: 3, unit: 'months' };
expect(durationDrafter(period)).toBe('3 months');
});

test('should format Duration with singular unit', () => {
const duration = { amount: 1, unit: 'day' };
expect(durationDrafter(duration)).toBe('1 day');
});
});
4 changes: 2 additions & 2 deletions test/__snapshots__/TemplateArchiveProcessor.test.ts.snap
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@ exports[`template archive processor should draft a template 1`] = `
"Late Delivery and Penalty
----

In case of delayed delivery except for Force Majeure cases, the Seller shall pay to the Buyer for every {"$class":"org.accordproject.time@0.3.0.Duration","amount":2,"unit":"days"} of delay penalty amounting to 10.5% of the total value of the Equipment whose delivery has been delayed.
In case of delayed delivery except for Force Majeure cases, the Seller shall pay to the Buyer for every 2 days of delay penalty amounting to 10.5% of the total value of the Equipment whose delivery has been delayed.
1. Any fractional part of a days is to be considered a full days.
2. The total amount of penalty shall not however, exceed 55.0% of the total value of the Equipment involved in late delivery.
3. If the delay is more than {"$class":"org.accordproject.time@0.3.0.Duration","amount":15,"unit":"days"}, the Buyer is entitled to terminate this Contract."
3. If the delay is more than 15 days, the Buyer is entitled to terminate this Contract."
`;