Skip to content

Commit

Permalink
feat: minor performance improvement
Browse files Browse the repository at this point in the history
  • Loading branch information
onhate committed Mar 4, 2024
1 parent d466a0a commit cdcb640
Show file tree
Hide file tree
Showing 3 changed files with 6 additions and 4 deletions.
4 changes: 2 additions & 2 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -96,11 +96,11 @@ export function relativeToDate(expression, currentDate = new Date()) {
throw new TypeError('The expression should be a string');
}

if (!isValid(expression)) {
const match = expression.match(regExp);
if (!match) {
throw new Error('The expression is not a valid expression');
}

const match = expression.match(regExp);
const operator = match[1] === '-' ? -1 : 1;
const amount = parseInt(match[2]);
const unit = units[match[3]];
Expand Down
4 changes: 3 additions & 1 deletion index.test.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import assert from 'node:assert';
import { describe, it } from 'node:test';
import { relativeToDate } from './index.js';
import { isValid, relativeToDate } from './index.js';

describe('datelative', () => {
describe('Invalid expressions', () => {
Expand Down Expand Up @@ -111,6 +111,8 @@ describe('datelative', () => {
it(expression, () => {
const reference = new Date();
const expected = new Date(reference.getTime() + millis);

assert.deepEqual(isValid(expression), true);
assert.deepEqual(relativeToDate(expression, reference), expected);
});
});
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "datelative",
"version": "0.0.6",
"version": "0.0.7",
"description": "A library that converts relative string text to JavaScript dates",
"type": "module",

Expand Down

0 comments on commit cdcb640

Please sign in to comment.