Skip to content
This repository has been archived by the owner on May 21, 2020. It is now read-only.

Commit

Permalink
Merge pull request #1 from etcdigital/v1
Browse files Browse the repository at this point in the history
V1
  • Loading branch information
etc-tiago authored May 10, 2020
2 parents a988642 + 3414491 commit 48864a7
Show file tree
Hide file tree
Showing 11 changed files with 4,369 additions and 0 deletions.
9 changes: 9 additions & 0 deletions .dependabot/config.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
version: 1
update_configs:
- package_manager: "javascript"
directory: "/"
update_schedule: "live"
default_reviewers:
- "etc-tiago"
default_labels:
- "dependencies"
16 changes: 16 additions & 0 deletions .github/workflows/changelog.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
name: Changelog Generator
on:
pull_request:
branches:
- master
types: [opened, reopened, synchronize]

jobs:
changelog:
name: Chanegelog Generator
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v2
- uses: etcdigital/pull-request-changelog@1.1
with:
token: ${{ secrets.GITHUB_TOKEN }}
27 changes: 27 additions & 0 deletions .github/workflows/publish.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
name: Publish to NPM

on:
release:
types: [created]

jobs:
deploy:
runs-on: ubuntu-latest

steps:
- uses: actions/setup-node@v1
with:
node-version: 12

- uses: actions/checkout@v1

- run: |
npm set progress=false
npm set package-lock=false
npm set registry https://registry.npmjs.org/ && echo "//registry.npmjs.org/:_authToken=${{ secrets.NPM_AUTH_TOKEN }}" > .npmrc
- run: yarn install --silent --frozen-lockfile

- run: yarn build

- run: npm publish --access public
27 changes: 27 additions & 0 deletions .github/workflows/tests.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
name: Tests

on:
pull_request:
branches:
- master

jobs:
test:
runs-on: ubuntu-latest
steps:
- uses: actions/setup-node@v1
with:
node-version: "12.16.x"
- uses: actions/checkout@v1

- name: Install
run: yarn install --silent --frozen-lockfile

- name: Lint
run: yarn lint

- name: Test
run: yarn test

- name: Build
run: yarn build
18 changes: 18 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
# time2input

A helper to manage 2 time inputs with increment/decrement on second input when the first changes.

## How to use

`npm install time2input --save-dev` or `yarn add -D time2input`.

```js
const inputTimeChange = require('time2input');
const beginTime = '08:00';
const endTime = '09:00';
const intervalInMinutes = 15;
const thisInputIs = 'begin'; // or "end"
const action = 'down'; // up to sum or down to substract
const newInputTimes = inputTimeChange(beginTime, endTime, intervalInMinutes, thisInputIs, action);
// newInputTimes = { begin: '07:45', end: '09:00' }
```
15 changes: 15 additions & 0 deletions jest.config.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
const { defaults } = require('jest-config');

module.exports = {
preset: 'ts-jest',
transform: { '^.+\\.ts?$': 'ts-jest' },
modulePaths: ['<rootDir>'],
moduleFileExtensions: [...defaults.moduleFileExtensions, 'ts', 'json'],
testRegex: '(/__tests__/.*|(\\.|/)(test|spec))\\.(ts|tsx)$',
testPathIgnorePatterns: ['/node_modules/', '/tests/fixture', '/tests/config', '/build/', '/dist/', '/coverage/'],
globals: { 'ts-jest': { tsConfig: '<rootDir>/tsconfig.json', diagnostics: false } },
testEnvironment: 'node',
collectCoverage: true,
collectCoverageFrom: ['src/**/*.{js,jsx,ts,tsx}', '!**/node_modules/**', '!**/build/**', '!**/coverage/**', '!**/tests/**'],
coverageReporters: ['lcov', 'text', 'text-summary'],
};
44 changes: 44 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
{
"name": "time2input",
"description": "A helper to manage 2 time inputs with increment/decrement",
"version": "1.0.0",
"author": "Tiago Souza",
"license": "MIT",
"scripts": {
"build": "tsc -p tsconfig.json",
"lint": "tslint -p tsconfig.json",
"test": "jest"
},
"engines": {
"node": ">12.16.0"
},
"repository": {
"url": "https://github.com/etcdigital/time2input",
"type": "git"
},
"files": [
"dist/**/*"
],
"main": "dist/index.js",
"types": "dist/index.d.ts",
"publishConfig": {
"access": "public"
},
"dependencies": {
"date-fns": "^2.13.0"
},
"devDependencies": {
"@netflix/tslint-config": "^1.0.1",
"@types/jest": "^25.1.3",
"@types/node": "^13.7.7",
"import-sort-style-eslint": "^6.0.0",
"jest": "^26.0.1",
"prettier": "^2.0.5",
"ts-jest": "^25.2.1",
"ts-node": "^8.6.2",
"tslint": "^6.0.0",
"tslint-config-prettier": "^1.18.0",
"tslint-plugin-prettier": "^2.2.0",
"typescript": "^3.8.3"
}
}
37 changes: 37 additions & 0 deletions src/index.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
import inputTimeChange from './';

describe('Lib inputTimeChange', () => {
it('Shoult increase beginBase', () => {
const result = inputTimeChange('08:00', '09:00', 15, 'begin', 'up');
expect(result).toStrictEqual({ begin: '08:15', end: '09:00' });
});

it('Shoult decrease beginBase', () => {
const result = inputTimeChange('08:00', '09:00', 15, 'begin', 'down');
expect(result).toStrictEqual({ begin: '07:45', end: '09:00' });
});

// #

it('Shoult increase endBase', () => {
const result = inputTimeChange('08:00', '09:00', 15, 'end', 'up');
expect(result).toStrictEqual({ begin: '08:00', end: '09:15' });
});

it('Shoult decrease endBase', () => {
const result = inputTimeChange('08:00', '09:00', 15, 'end', 'down');
expect(result).toStrictEqual({ begin: '08:00', end: '08:45' });
});

// #

it('Shoult increase beginBase and endBase when change begin', () => {
const result = inputTimeChange('08:00', '08:00', 15, 'begin', 'up');
expect(result).toStrictEqual({ begin: '08:15', end: '08:30' });
});

it('Shoult increase beginBase and endBase when change end', () => {
const result = inputTimeChange('08:00', '08:00', 15, 'end', 'down');
expect(result).toStrictEqual({ begin: '07:30', end: '07:45' });
});
});
53 changes: 53 additions & 0 deletions src/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
import { addMinutes, subMinutes } from 'date-fns';

const addSecondDigit = (digit: number): string => (digit < 10 ? `0${digit}` : digit.toString());

export const getTimeString = (date: Date): string => `${addSecondDigit(date.getHours())}:${addSecondDigit(date.getMinutes())}`;

export const inputPattern = '([0-1]{1}[0-9]{1}|20|21|22|23):[0-5]{1}[0-9]{1}';

/* Define types */
export type ITimeChangeId = 'begin' | 'end';
export type ITimeChangeAction = 'up' | 'down';

/**
* Update value of date, increase/decrease minutes
*/
const timeChange = (date: Date, method: ITimeChangeAction, minutes: number): Date => {
if (method === 'up') {
return addMinutes(date, minutes);
}
return subMinutes(date, minutes);
};

/* Turn time string to date */
const createDate = (time: string) => {
const value = time.split(':');
return new Date(2020, 2, 2, parseInt(value[0], 10), parseInt(value[1], 10));
};

const inputTimeChange = (
beginBase: string,
endBase: string,
minutes: number,
changed: ITimeChangeId,
method: ITimeChangeAction,
): { begin: string; end: string } => {
let begin = createDate(beginBase);
let end = createDate(endBase);

if (changed === 'begin') {
begin = timeChange(begin, method, minutes);
} else {
end = timeChange(end, method, minutes);
}

if (changed === 'begin' && begin.getTime() >= end.getTime()) {
end = timeChange(begin, 'up', minutes);
} else if (changed === 'end' && end.getTime() <= begin.getTime()) {
begin = timeChange(end, 'down', minutes);
}
return { begin: getTimeString(begin), end: getTimeString(end) };
};

export default inputTimeChange;
4 changes: 4 additions & 0 deletions src/typings.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
declare module '*.json' {
const value: any;
export default value;
}
Loading

0 comments on commit 48864a7

Please sign in to comment.