Skip to content

Commit

Permalink
feat: initial commit
Browse files Browse the repository at this point in the history
  • Loading branch information
onhate committed Mar 1, 2024
0 parents commit 522c334
Show file tree
Hide file tree
Showing 10 changed files with 443 additions and 0 deletions.
19 changes: 19 additions & 0 deletions .github/workflows/ci.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
name: Run Tests

on:
push:
branches: [ * ]
pull_request:
branches: [ * ]

jobs:
test:
runs-on: ubuntu-latest

steps:
- uses: actions/checkout@v2
- uses: actions/setup-node@v2
with:
node-version: 20
- run: npm ci
- run: npm test
132 changes: 132 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,132 @@
### Node template
# Logs
logs
*.log
npm-debug.log*
yarn-debug.log*
yarn-error.log*
lerna-debug.log*
.pnpm-debug.log*

# Diagnostic reports (https://nodejs.org/api/report.html)
report.[0-9]*.[0-9]*.[0-9]*.[0-9]*.json

# Runtime data
pids
*.pid
*.seed
*.pid.lock

# Directory for instrumented libs generated by jscoverage/JSCover
lib-cov

# Coverage directory used by tools like istanbul
coverage
*.lcov

# nyc test coverage
.nyc_output

# Grunt intermediate storage (https://gruntjs.com/creating-plugins#storing-task-files)
.grunt

# Bower dependency directory (https://bower.io/)
bower_components

# node-waf configuration
.lock-wscript

# Compiled binary addons (https://nodejs.org/api/addons.html)
build/Release

# Dependency directories
node_modules/
jspm_packages/

# Snowpack dependency directory (https://snowpack.dev/)
web_modules/

# TypeScript cache
*.tsbuildinfo

# Optional npm cache directory
.npm

# Optional eslint cache
.eslintcache

# Optional stylelint cache
.stylelintcache

# Microbundle cache
.rpt2_cache/
.rts2_cache_cjs/
.rts2_cache_es/
.rts2_cache_umd/

# Optional REPL history
.node_repl_history

# Output of 'npm pack'
*.tgz

# Yarn Integrity file
.yarn-integrity

# dotenv environment variable files
.env
.env.development.local
.env.test.local
.env.production.local
.env.local

# parcel-bundler cache (https://parceljs.org/)
.cache
.parcel-cache

# Next.js build output
.next
out

# Nuxt.js build / generate output
.nuxt
dist

# Gatsby files
.cache/
# Comment in the public line in if your project uses Gatsby and not Next.js
# https://nextjs.org/blog/next-9-1#public-directory-support
# public

# vuepress build output
.vuepress/dist

# vuepress v2.x temp and cache directory
.temp
.cache

# Docusaurus cache and generated files
.docusaurus

# Serverless directories
.serverless/

# FuseBox cache
.fusebox/

# DynamoDB Local files
.dynamodb/

# TernJS port file
.tern-port

# Stores VSCode versions used for testing VSCode extensions
.vscode-test

# yarn v2
.yarn/cache
.yarn/unplugged
.yarn/build-state.yml
.yarn/install-state.gz
.pnp.*

5 changes: 5 additions & 0 deletions .idea/.gitignore

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

12 changes: 12 additions & 0 deletions .idea/datelative.iml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 6 additions & 0 deletions .idea/jsLibraryMappings.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

8 changes: 8 additions & 0 deletions .idea/modules.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 6 additions & 0 deletions .idea/vcs.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

110 changes: 110 additions & 0 deletions index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,110 @@
const units = {
'ms': 'milliseconds',
'milliseconds': 'milliseconds',
's': 'seconds',
'sec': 'seconds',
'second': 'seconds',
'seconds': 'seconds',
'Second': 'seconds',
'Seconds': 'seconds',
'm': 'minutes',
'min': 'minutes',
'minute': 'minutes',
'minutes': 'minutes',
'Min': 'minutes',
'Minute': 'minutes',
'Minutes': 'minutes',
'h': 'hours',
'hr': 'hours',
'Hr': 'hours',
'hour': 'hours',
'hours': 'hours',
'Hour': 'hours',
'Hours': 'hours',
'd': 'days',
'D': 'days',
'day': 'days',
'days': 'days',
'Day': 'days',
'Days': 'days',
'w': 'weeks',
'W': 'weeks',
'wk': 'weeks',
'Wk': 'weeks',
'week': 'weeks',
'weeks': 'weeks',
'Week': 'weeks',
'Weeks': 'weeks',
'M': 'months',
'mon': 'months',
'month': 'months',
'months': 'months',
'Month': 'months',
'Months': 'months',
'y': 'years',
'Y': 'years',
'yr': 'years',
'yrs': 'years',
'year': 'years',
'years': 'years',
'Year': 'years',
'Years': 'years',
'q': 'quarters',
'qr': 'quarters',
'Q': 'quarters',
'Qr': 'quarters',
'qtr': 'quarters',
'quarter': 'quarters',
'quarters': 'quarters',
'Quarter': 'quarters',
'Quarters': 'quarters'
};

const unitToMilliseconds = {
'milliseconds': 1,
'seconds': 1000,
'minutes': 1000 * 60,
'hours': 1000 * 60 * 60,
'days': 1000 * 60 * 60 * 24,
'weeks': 1000 * 60 * 60 * 24 * 7,
'months': 1000 * 60 * 60 * 24 * 30, // Approximation
'years': 1000 * 60 * 60 * 24 * 365, // Approximation
'quarters': 1000 * 60 * 60 * 24 * 365 / 4 // Approximation
};

const parseableUnits = Object.keys(units).join('|');
const regExp = new RegExp(`([-+])?(\\d*)\\s?(${parseableUnits})$`);

/**
* Checks if a time expression is valid
* @param expression {string} - The time expression to check
* @return {boolean}
*/
export function isValid(expression) {
return regExp.test(expression);
}

/**
* Converts a time expression to a Date object
* @param expression {string} - The time expression to convert
* @param currentDate {Date} - The current date to use as a reference
* @return {Date|null}
*/
export function relativeToDate(expression, currentDate = new Date()) {
if (typeof expression !== 'string') {
throw new TypeError('The expression should be a string');
}

if (!isValid(expression)) {
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]];
const milliseconds = amount * unitToMilliseconds[unit];

const time = currentDate.getTime() + (operator * milliseconds);
return new Date(time);
}
Loading

0 comments on commit 522c334

Please sign in to comment.