@libchrono/time 1.0.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/LICENSE ADDED
@@ -0,0 +1,9 @@
1
+ MIT License
2
+
3
+ Copyright (c) 2025 Damian WileΕ„ski
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
6
+
7
+ The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
8
+
9
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
package/README.md ADDED
@@ -0,0 +1,90 @@
1
+ # πŸ•’ @libchrono/time
2
+
3
+ [![npm version](https://badge.fury.io/js/@libchrono%2Ftime.svg)](https://badge.fury.io/js/@libchrono%2Ftime)
4
+ [![GitHub license](https://img.shields.io/github/license/Naereen/StrapDown.js.svg)](https://github.com/Naereen/StrapDown.js/blob/master/LICENSE)
5
+ ![](coverage/badge.svg)
6
+
7
+ ## ⏳ Simple Time Format Converter
8
+
9
+ @libchrono/time is a lightweight and zero-dependency JavaScript library that helps you effortlessly:
10
+
11
+ βœ… Convert 24-hour (military) time to 12-hour (meridiem) time
12
+
13
+ βœ… Convert 12-hour (meridiem) time to 24-hour (military) time
14
+
15
+ βœ… Validate if a string is a valid military or civilian time format
16
+
17
+ βœ… Extract each component of time string in both formats
18
+
19
+ ## πŸ“¦ Installation
20
+
21
+ To use @libchrono/time in your project, install it via npm:
22
+
23
+ ```bash
24
+ npm install @libchrono/time
25
+ ```
26
+
27
+ or yarn:
28
+
29
+ ```bash
30
+ yarn add @libchrono/time
31
+ ```
32
+
33
+ ```typescript
34
+ const { convertMeridiemToMilitary } = require('@libchrono/time');
35
+ // or using ES Modules
36
+ import { convertMeridiemToMilitary } from '@libchrono/time';
37
+
38
+ console.log(convertMeridiemToMilitary('10:00 AM')); // result: 10:00
39
+ ```
40
+
41
+ ## πŸ”§ API Reference
42
+
43
+ ### Convert 24-hour time to 12-hour time
44
+
45
+ ```typescript
46
+ console.log(convertMilitaryToMeridiem('23:45')); // Output: "11:45 PM"
47
+ console.log(convertMilitaryToMeridiem('09:30')); // Output: "9:30 AM"
48
+ ```
49
+
50
+ ### Convert 12-hour time to 24-hour time
51
+
52
+ ```typescript
53
+ console.log(convertMeridiemToMilitary('11:45 PM')); // Output: "23:45"
54
+ console.log(convertMeridiemToMilitary('9:30 AM')); // Output: "09:30"
55
+ ```
56
+
57
+ ### Validate if a string is in military (24-hour) time format
58
+
59
+ ```typescript
60
+ console.log(isMilitaryFormat('23:45')); // Output: true
61
+ console.log(isMilitaryFormat('11:45 PM')); // Output: false
62
+ console.log(isMilitaryFormat('25:30')); // Output: false (invalid time)
63
+ ```
64
+
65
+ ### Validate if a string is in civilian (12-hour) time format
66
+
67
+ ```typescript
68
+ console.log(isMeridiemFormat('11:45 PM')); // Output: true
69
+ console.log(isMeridiemFormat('23:45')); // Output: false
70
+ console.log(isMeridiemFormat('13:30 PM')); // Output: false (invalid time)
71
+ ```
72
+
73
+ ### Extract components of the time string
74
+
75
+ ```typescript
76
+ console.log(getMeridiemComponents('11:45 PM')); // Output: { hour: 11, minute: 45, period: 'PM' }
77
+ console.log(getMilitaryComponents('23:45')); // Output: { hour: 23, minute: 45 }
78
+ ```
79
+
80
+ [Full documentation](./docs/globals.md)
81
+
82
+ ## πŸ› οΈ Why Use @libchrono/time
83
+
84
+ βœ… Zero dependencies – lightweight and efficient.
85
+
86
+ βœ… Simple API – minimal, intuitive functions.
87
+
88
+ βœ… Accurate conversions & validations – ensures correctness.
89
+
90
+ βœ… Fully tested – reliable with unit tests.
@@ -0,0 +1,32 @@
1
+ /**
2
+ * Verifies if provided time string is in valid 24-hour (military) time format
3
+ * @param value Time string
4
+ * @returns
5
+ */
6
+ export declare const isMilitaryFormat: (value: string) => boolean;
7
+ /**
8
+ * Verifies if provided time string is in valid 12-hour (meridiem) time format
9
+ * @param value Time string
10
+ * @returns
11
+ */
12
+ export declare const isMeridiemFormat: (value: string) => boolean;
13
+ /**
14
+ * Verifies if provided string is a valid AM period
15
+ * @param value Period string
16
+ * @returns
17
+ */
18
+ export declare const isPreNoonPeriodStr: (value: string) => boolean;
19
+ /**
20
+ * Verifies if provided string is a valid AM period. Alias for {@link isPreNoonPeriodStr}
21
+ */
22
+ export declare const isAMPeriodStr: (value: string) => boolean;
23
+ /**
24
+ * Verifies if provided string is a valid PM period
25
+ * @param value Period string
26
+ * @returns
27
+ */
28
+ export declare const isPostNoonPeriodStr: (value: string) => boolean;
29
+ /**
30
+ * Verifies if provided string is a valid PM period. Alias for {@link isPostNoonPeriodStr}
31
+ */
32
+ export declare const isPMPeriodStr: (value: string) => boolean;
@@ -0,0 +1,38 @@
1
+ /**
2
+ * Converts 12-hour (meridiem) time string to 24-hour (military) time string.
3
+ * @param value Meridiem time string
4
+ * @returns Time in military format
5
+ */
6
+ export declare const convertMeridiemToMilitary: (value: string) => string;
7
+ /**
8
+ * Converts 12-hour (meridiem) time string to 24-hour (military) time string.
9
+ * Alias for {@link convertMeridiemToMilitary}.
10
+ */
11
+ export declare const convertCivilToMilitary: (value: string) => string;
12
+ /**
13
+ * Converts 12-hour (meridiem) time string to 24-hour (military) time string.
14
+ * Alias for {@link convertMeridiemToMilitary}.
15
+ */
16
+ export declare const convert12To24: (value: string) => string;
17
+ /**
18
+ * Converts 12-hour (meridiem) time string to 24-hour (military) time string.
19
+ * Alias for {@link convertMeridiemToMilitary}.
20
+ */
21
+ export declare const toMilitary: (value: string) => string;
22
+ /**
23
+ * Converts 24-hour (military) time string to 12-hour (meridiem) time string.
24
+ * @param value Military time string
25
+ * @returns Time in meridiem format
26
+ */
27
+ export declare const convertMilitaryToMeridiem: (value: string) => string;
28
+ /**
29
+ * Converts 24-hour (military) time string to 12-hour (meridiem) time string.
30
+ * Alias for {@link convertMilitaryToMeridiem}.
31
+ */
32
+ export declare const convertMilitaryToCivil: (value: string) => string;
33
+ /**
34
+ * Converts 24-hour (military) time string to 12-hour (meridiem) time string.
35
+ * Alias for {@link convertMilitaryToMeridiem}.
36
+ */
37
+ export declare const convert24To12: (value: string) => string;
38
+ export declare const toMeridiem: (value: string) => string;
@@ -0,0 +1,14 @@
1
+ import { MeridiemTimeComponents } from './type/meridiem-time-components';
2
+ import { MilitaryTimeComponents } from './type/military-time-components';
3
+ /**
4
+ * Extracts time elements from provided valid military time string. If provided string is invalid method should throw error.
5
+ * @param value Time string
6
+ * @returns
7
+ */
8
+ export declare const getMilitaryComponents: (value: string) => MilitaryTimeComponents;
9
+ /**
10
+ * Extracts time elements from provided valid meridiem time string. If provided string is invalid method should throw error.
11
+ * @param value Time string
12
+ * @returns
13
+ */
14
+ export declare const getMeridiemComponents: (value: string) => MeridiemTimeComponents;
@@ -0,0 +1,7 @@
1
+ export * from "./type/enum/meridiem";
2
+ export * from "./type/enum/time-component";
3
+ export * from "./type/meridiem-time-components";
4
+ export * from "./type/military-time-components";
5
+ export * from "./checkers";
6
+ export * from "./converters";
7
+ export * from "./extractors";
package/dist/index.js ADDED
@@ -0,0 +1 @@
1
+ !function(e,i){"object"===typeof exports&&"object"===typeof module?module.exports=i():"function"===typeof define&&define.amd?define([],i):"object"===typeof exports?exports["libchrono-time"]=i():e["libchrono-time"]=i()}(this,(()=>(()=>{"use strict";var e={721:(e,i,r)=>{Object.defineProperty(i,"__esModule",{value:!0}),i.isPMPeriodStr=i.isPostNoonPeriodStr=i.isAMPeriodStr=i.isPreNoonPeriodStr=i.isMeridiemFormat=i.isMilitaryFormat=void 0;const o=r(239);i.isMilitaryFormat=e=>o.MilitaryTimeRegex.test(e);i.isMeridiemFormat=e=>o.MeridiemTimeRegex.test(e);i.isPreNoonPeriodStr=e=>o.AMPeriodRegex.test(e),i.isAMPeriodStr=i.isPreNoonPeriodStr;i.isPostNoonPeriodStr=e=>o.PMPeriodRegex.test(e),i.isPMPeriodStr=i.isPostNoonPeriodStr},704:(e,i,r)=>{Object.defineProperty(i,"__esModule",{value:!0}),i.toMeridiem=i.convert24To12=i.convertMilitaryToCivil=i.convertMilitaryToMeridiem=i.toMilitary=i.convert12To24=i.convertCivilToMilitary=i.convertMeridiemToMilitary=void 0;const o=r(721),t=r(492),n=r(552),d=r(550);i.convertMeridiemToMilitary=e=>{if(!(0,o.isMeridiemFormat)(e))throw new Error(`Provided invalid format to convert: ${e}`);const i=(0,t.getMeridiemComponents)(e),r=(e=>e.hour%12+(e.period===d.Meridiem.PM?12:0))(i);return m=r,M=i.minute,`${(0,n.padTimeNumber)(m)}:${(0,n.padTimeNumber)(M)}`;var m,M},i.convertCivilToMilitary=i.convertMeridiemToMilitary,i.convert12To24=i.convertMeridiemToMilitary,i.toMilitary=i.convertMeridiemToMilitary;i.convertMilitaryToMeridiem=e=>{if(!(0,o.isMilitaryFormat)(e))throw new Error(`Provided invalid format to convert: ${e}`);const i=(0,t.getMilitaryComponents)(e),r=i.hour%12||12,m=i.hour>=12?d.Meridiem.PM:d.Meridiem.AM;return((e,i,r)=>`${(0,n.padTimeNumber)(e)}:${(0,n.padTimeNumber)(i)} ${r}`)(r,i.minute,m)},i.convertMilitaryToCivil=i.convertMilitaryToMeridiem,i.convert24To12=i.convertMilitaryToMeridiem,i.toMeridiem=i.convertMilitaryToMeridiem},492:(e,i,r)=>{Object.defineProperty(i,"__esModule",{value:!0}),i.getMeridiemComponents=i.getMilitaryComponents=void 0;const o=r(721),t=r(239),n=r(550),d=r(707),m=e=>(0,o.isPreNoonPeriodStr)(e)?n.Meridiem.AM:n.Meridiem.PM;i.getMilitaryComponents=e=>{const i=e.match(t.MilitaryTimeRegex);if(null===i)throw new Error(`Provided invalid time value: ${e}`);return{[d.TimeComponent.Hour]:parseInt(i[1],10),[d.TimeComponent.Minute]:parseInt(i[2],10)}};i.getMeridiemComponents=e=>{const i=e.match(t.MeridiemTimeRegex);if(null===i)throw new Error(`Provided invalid time value: ${e}`);return{[d.TimeComponent.Hour]:parseInt(i[1],10),[d.TimeComponent.Minute]:parseInt(i[2],10),[d.TimeComponent.Period]:m(i[3])}}},729:function(e,i,r){var o=this&&this.__createBinding||(Object.create?function(e,i,r,o){void 0===o&&(o=r);var t=Object.getOwnPropertyDescriptor(i,r);t&&!("get"in t?!i.__esModule:t.writable||t.configurable)||(t={enumerable:!0,get:function(){return i[r]}}),Object.defineProperty(e,o,t)}:function(e,i,r,o){void 0===o&&(o=r),e[o]=i[r]}),t=this&&this.__exportStar||function(e,i){for(var r in e)"default"===r||Object.prototype.hasOwnProperty.call(i,r)||o(i,e,r)};Object.defineProperty(i,"__esModule",{value:!0}),t(r(550),i),t(r(707),i),t(r(51),i),t(r(542),i),t(r(721),i),t(r(704),i),t(r(492),i)},239:(e,i)=>{Object.defineProperty(i,"__esModule",{value:!0}),i.PMPeriodRegex=i.AMPeriodRegex=i.MeridiemTimeRegex=i.MilitaryTimeRegex=void 0,i.MilitaryTimeRegex=/^([01]?\d|2[0-3])\D?([0-5][0-9])$/,i.MeridiemTimeRegex=/^(0?[1-9]|1[0-2]):([0-5][0-9])\s?(am|pm|a\.m\.|p\.m\.|a\.m|p\.m|a m|p m)$/i,i.AMPeriodRegex=/\bam|\ba\.m\.|\ba\.m\b/i,i.PMPeriodRegex=/\bpm|\bp\.m\.|\bp\.m\b/i},550:(e,i)=>{var r,o,t,n;Object.defineProperty(i,"__esModule",{value:!0}),i.CivilianTimePeriod=i.TimeOfDay=i.DayPeriod=i.Meridiem=void 0,function(e){e.AM="AM",e.PM="PM"}(r||(i.Meridiem=r={})),function(e){e.AM="AM",e.PM="PM"}(o||(i.DayPeriod=o={})),function(e){e.AM="AM",e.PM="PM"}(t||(i.TimeOfDay=t={})),function(e){e.AM="AM",e.PM="PM"}(n||(i.CivilianTimePeriod=n={}));o.AM},707:(e,i)=>{var r;Object.defineProperty(i,"__esModule",{value:!0}),i.TimeComponent=void 0,function(e){e.Hour="hour",e.Minute="minute",e.Period="period"}(r||(i.TimeComponent=r={}))},51:(e,i,r)=>{Object.defineProperty(i,"__esModule",{value:!0});r(707)},542:(e,i,r)=>{Object.defineProperty(i,"__esModule",{value:!0});r(707)},552:(e,i)=>{Object.defineProperty(i,"__esModule",{value:!0}),i.padTimeNumber=void 0;i.padTimeNumber=e=>{if(e<0)throw new Error("Time number cant be negative");if(e>59)throw new Error("Time number cant be higher then 59");return e<10?`0${e}`:`${e}`}}},i={};var r=function r(o){var t=i[o];if(void 0!==t)return t.exports;var n=i[o]={exports:{}};return e[o].call(n.exports,n,n.exports,r),n.exports}(729);return r})()));
@@ -0,0 +1,16 @@
1
+ /**
2
+ * Match the military format with option to select hours at 1 index and minutes and 2 index.
3
+ */
4
+ export declare const MilitaryTimeRegex: RegExp;
5
+ /**
6
+ * Match the meridiem format with option to select hours at 1 index, minutes at 2 index and period at 3 index.
7
+ */
8
+ export declare const MeridiemTimeRegex: RegExp;
9
+ /**
10
+ * Match the ante-meridiem (pre-noon) period name.
11
+ */
12
+ export declare const AMPeriodRegex: RegExp;
13
+ /**
14
+ * Match the post-meridiem (post-noon) period name.
15
+ */
16
+ export declare const PMPeriodRegex: RegExp;
@@ -0,0 +1,34 @@
1
+ /**
2
+ * Enumerator representing periods in 12-hour (meridiem) time.
3
+ */
4
+ export declare enum Meridiem {
5
+ /**
6
+ * Value representing pre-noon (ante-meridiem).
7
+ */
8
+ AM = "AM",
9
+ /**
10
+ * Value representing pre-noon (post-meridiem).
11
+ */
12
+ PM = "PM"
13
+ }
14
+ /**
15
+ * Enumerator representing periods in 12-hour (meridiem) time. Alias for {@link Meridiem}.
16
+ */
17
+ export declare enum DayPeriod {
18
+ AM = "AM",
19
+ PM = "PM"
20
+ }
21
+ /**
22
+ * Enumerator representing periods in 12-hour (meridiem) time. Alias for {@link Meridiem}.
23
+ */
24
+ export declare enum TimeOfDay {
25
+ AM = "AM",
26
+ PM = "PM"
27
+ }
28
+ /**
29
+ * Enumerator representing periods in 12-hour (meridiem) time. Alias for {@link Meridiem}.
30
+ */
31
+ export declare enum CivilianTimePeriod {
32
+ AM = "AM",
33
+ PM = "PM"
34
+ }
@@ -0,0 +1,17 @@
1
+ /**
2
+ * Enumerator representing components from which the time string is built.
3
+ */
4
+ export declare enum TimeComponent {
5
+ /**
6
+ * 00-23 or 1-12
7
+ */
8
+ Hour = "hour",
9
+ /**
10
+ * 00-59
11
+ */
12
+ Minute = "minute",
13
+ /**
14
+ * AM or PM
15
+ */
16
+ Period = "period"
17
+ }
@@ -0,0 +1,10 @@
1
+ import { TimeComponent } from './enum/time-component';
2
+ import { Meridiem } from './enum/meridiem';
3
+ /**
4
+ * Model representing components of the meridiem time string.
5
+ */
6
+ export interface MeridiemTimeComponents {
7
+ readonly [TimeComponent.Hour]: number;
8
+ readonly [TimeComponent.Minute]: number;
9
+ readonly [TimeComponent.Period]: Meridiem;
10
+ }
@@ -0,0 +1,8 @@
1
+ import { TimeComponent } from './enum/time-component';
2
+ /**
3
+ * Model representing components of the military time string.
4
+ */
5
+ export interface MilitaryTimeComponents {
6
+ readonly [TimeComponent.Hour]: number;
7
+ readonly [TimeComponent.Minute]: number;
8
+ }
@@ -0,0 +1,6 @@
1
+ /**
2
+ * Method add `0` prefix for numbers that have only one character. Should throw error when provided value is negative.
3
+ * @param value Time number
4
+ * @returns
5
+ */
6
+ export declare const padTimeNumber: (value: number) => string;
package/package.json ADDED
@@ -0,0 +1,74 @@
1
+ {
2
+ "name": "@libchrono/time",
3
+ "version": "1.0.0",
4
+ "description": "Small library to handle time format conversion.",
5
+ "main": "dist/index.js",
6
+ "types": "dist/index.d.ts",
7
+ "files": [
8
+ "dist/"
9
+ ],
10
+ "author": "Damian WileΕ„ski",
11
+ "license": "MIT",
12
+ "repository": {
13
+ "url": "git+https://github.com/damianw27/libchrono-time.git"
14
+ },
15
+ "keywords": [
16
+ "time",
17
+ "convert",
18
+ "timeformat",
19
+ "militarytime",
20
+ "meridiem",
21
+ "24h",
22
+ "12h",
23
+ "timeutils",
24
+ "timeparser",
25
+ "timeconversion",
26
+ "timeformatter",
27
+ "clock",
28
+ "hourformat",
29
+ "timelib",
30
+ "timekeeper",
31
+ "timeadjust",
32
+ "timetools",
33
+ "timetransform",
34
+ "timechanger",
35
+ "timeconvertor"
36
+ ],
37
+ "scripts": {
38
+ "build:lib": "webpack && tsc-alias -p tsconfig.json",
39
+ "build:types": "tsc -p tsconfig.type.json && tsc-alias -p tsconfig.type.json",
40
+ "build:all": "yarn build:lib && yarn build:types",
41
+ "test": "jest",
42
+ "coverage": "yarn test --coverage && make-coverage-badge",
43
+ "deploy": "yarn test && yarn build:all && npm publish",
44
+ "docs": "typedoc"
45
+ },
46
+ "devDependencies": {
47
+ "@types/jest": "^29.5.14",
48
+ "@types/node": "^22.12.0",
49
+ "@typescript-eslint/eslint-plugin": "^8.22.0",
50
+ "@typescript-eslint/parser": "^8.22.0",
51
+ "eslint": "^9.19.0",
52
+ "eslint-config-love": "^118.0.0",
53
+ "eslint-config-prettier": "^10.0.1",
54
+ "eslint-import-resolver-typescript": "^3.7.0",
55
+ "eslint-plugin-check-file": "^2.8.0",
56
+ "eslint-plugin-import": "^2.31.0",
57
+ "eslint-plugin-max-params-no-constructor": "^0.0.4",
58
+ "eslint-webpack-plugin": "^4.2.0",
59
+ "jest": "^29.7.0",
60
+ "make-coverage-badge": "^1.2.0",
61
+ "prettier": "^3.4.2",
62
+ "terser-webpack-plugin": "^5.3.11",
63
+ "ts-jest": "^29.2.5",
64
+ "ts-loader": "^9.5.2",
65
+ "ts-node": "^10.9.2",
66
+ "tsc-alias": "^1.8.10",
67
+ "tsconfig-paths-webpack-plugin": "^4.2.0",
68
+ "typedoc": "^0.27.6",
69
+ "typedoc-plugin-markdown": "^4.4.1",
70
+ "typescript": "^5.7.3",
71
+ "webpack": "^5.97.1",
72
+ "webpack-cli": "^6.0.1"
73
+ }
74
+ }