@oluwanifemi/date-formatt 0.0.1 → 0.0.2

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.
Files changed (3) hide show
  1. package/index.js +36 -40
  2. package/package.json +1 -1
  3. package/validator.js +43 -0
package/index.js CHANGED
@@ -1,61 +1,57 @@
1
- const { module } = require("browserify/lib/builtins")
1
+ const validator = require("./validator")
2
2
 
3
- const date = new Date()
4
- // console.log(date) --> this returns a date in the ISO 8601 format,
5
- // commonly used in databases, APIs and JavaScript
6
-
7
- // e.g "2026-03-09T11:42:13.260Z"
8
- // Date portion --> "YYYY-MM-DD" --> "2026-03-09"
9
- // Time portion --> "HH:MM:SS.milliseconds" --> "11:42:13.260Z"
10
- // T --> Indicator for time portion starting point
11
- // Z --> "Zulu Time"/"Coordinated Universal Time (UTC)"
12
-
13
- const d = new Date("October 13, 2014 11:13:00")
14
- // console.log(d) --> this also returns the date in ISO 8601 format
15
- // C:\Users\FullstackCreator\Documents\Programming\date-formatt
16
-
17
- function iso(date) {
18
- return date.toISOString().split("T")[0]
3
+ function iso(input) {
4
+ if(typeof(input) === 'string'){
5
+ let date = validator.validate(input)
6
+ return date.toISOString().split("T")[0]
7
+ }
19
8
  }
20
9
 
21
- function short(date) {
22
- return date.toLocaleDateString("en", {
10
+ function short(input) {
11
+ if(typeof(input) === 'string'){
12
+ let date = validator.validate(input)
13
+ return date.toLocaleDateString("en", {
23
14
  day: "numeric",
24
15
  month: "short",
25
16
  year: "numeric"
26
17
  })
18
+ }
19
+
27
20
  }
28
21
 
29
- function long(date) {
30
- return date.toLocaleDateString("en", {
22
+ function long(input) {
23
+ if(typeof(input) === 'string') {
24
+ let date = validator.validate(input)
25
+ return date.toLocaleDateString("en", {
31
26
  weekday: "long",
32
27
  month: "long",
33
28
  day: "numeric",
34
29
  year: "numeric"
35
30
  })
31
+ }
36
32
  }
37
33
 
38
- function dateTime(date) {
39
- return date.toLocaleString("en", {
40
- day: "numeric",
41
- month: "short",
42
- year: "numeric",
43
- hour: "numeric",
44
- minute: "2-digit"
45
- })
46
- }
47
-
48
- function time(date) {
49
- return date.toLocaleTimeString("en", {
50
- hour: "numeric",
51
- minute: "2-digit"
52
- })
53
- }
54
34
 
55
35
  module.exports = {
56
36
  iso,
57
37
  short,
58
38
  long,
59
- dateTime,
60
- time
61
- }
39
+ }
40
+
41
+
42
+
43
+ // const { module } = require("browserify/lib/builtins")
44
+
45
+ // const date = new Date()
46
+ // console.log(date) --> this returns a date in the ISO 8601 format,
47
+ // commonly used in databases, APIs and JavaScript
48
+
49
+ // e.g "2026-03-09T11:42:13.260Z"
50
+ // Date portion --> "YYYY-MM-DD" --> "2026-03-09"
51
+ // Time portion --> "HH:MM:SS.milliseconds" --> "11:42:13.260Z"
52
+ // T --> Indicator for time portion starting point
53
+ // Z --> "Zulu Time"/"Coordinated Universal Time (UTC)"
54
+
55
+ // const d = new Date("October 13, 2014 11:13:00")
56
+ // console.log(d) --> this also returns the date in ISO 8601 format
57
+ // C:\Users\FullstackCreator\Documents\Programming\date-formatt
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@oluwanifemi/date-formatt",
3
- "version": "0.0.1",
3
+ "version": "0.0.2",
4
4
  "description": "A utility package that allows you to get dates in desired formats",
5
5
  "main": "index.js",
6
6
  "scripts": {
package/validator.js ADDED
@@ -0,0 +1,43 @@
1
+ const fakeDateError = [null, "", 'boolean', undefined]
2
+
3
+ function validate(input) {
4
+ const inValid = fakeDateError.some(errorType => input === errorType || typeof(input) === 'boolean')
5
+ if(inValid) {
6
+ throw new TypeError("Input cannot be a null, boolean or undefined.")
7
+ }
8
+
9
+
10
+ let inputArray
11
+
12
+ try {
13
+ inputArray = input.split('-')
14
+ } catch (error) {
15
+ throw new TypeError(error + ": Your input should be in the format 'YYYY - MM - DD'")
16
+ }
17
+
18
+ const year = inputArray[0]
19
+ const month = inputArray[1]
20
+ const day = inputArray[2]
21
+
22
+ const date = new Date(input)
23
+
24
+ if(date.getFullYear() !== Number(year) || date.getMonth() +1 !== Number(month) || date.getDate() !== Number(day)){
25
+ console.log(date.getFullYear())
26
+ console.log(date.getMonth())
27
+ console.log(date.getDate())
28
+ console.log(Number(year))
29
+ console.log(Number(month))
30
+ console.log(Number(day))
31
+ throw new TypeError("Your input should be in the format 'YYYY - MM - DD' and not 'YYYY-DD-MM'")
32
+ }
33
+
34
+ if(Number.isNaN(date.getTime())) {
35
+ throw new TypeError("Input must be of a valid date type.")
36
+ }
37
+
38
+ return date
39
+ }
40
+
41
+ module.exports = {
42
+ validate
43
+ }