MCPcopy
hub / github.com/iamkun/dayjs / Duration

Class Duration

src/plugin/duration/index.js:60–259  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

58}
59
60class Duration {
61 constructor(input, unit, locale) {
62 this.$d = {}
63 this.$l = locale
64 if (input === undefined) {
65 this.$ms = 0
66 this.parseFromMilliseconds()
67 }
68 if (unit) {
69 return wrapper(input * unitToMS[prettyUnit(unit)], this)
70 }
71 if (typeof input === 'number') {
72 this.$ms = input
73 this.parseFromMilliseconds()
74 return this
75 }
76 if (typeof input === 'object') {
77 Object.keys(input).forEach((k) => {
78 this.$d[prettyUnit(k)] = input[k]
79 })
80 this.calMilliseconds()
81 return this
82 }
83 if (typeof input === 'string') {
84 const d = input.match(DURATION_REGEX_PARSE)
85 if (d) {
86 const properties = d.slice(2)
87 const numberD = properties.map(value => (value != null ? Number(value) : 0));
88 [
89 this.$d.years,
90 this.$d.months,
91 this.$d.weeks,
92 this.$d.days,
93 this.$d.hours,
94 this.$d.minutes,
95 this.$d.seconds
96 ] = numberD
97 this.calMilliseconds()
98 return this
99 }
100 }
101 return this
102 }
103
104 calMilliseconds() {
105 this.$ms = Object.keys(this.$d).reduce((total, unit) => (
106 total + ((this.$d[unit] || 0) * (unitToMS[unit]))
107 ), 0)
108 }
109
110 parseFromMilliseconds() {
111 let { $ms } = this
112 this.$d.years = roundNumber($ms / MILLISECONDS_A_YEAR)
113 $ms %= MILLISECONDS_A_YEAR
114 this.$d.months = roundNumber($ms / MILLISECONDS_A_MONTH)
115 $ms %= MILLISECONDS_A_MONTH
116 this.$d.days = roundNumber($ms / MILLISECONDS_A_DAY)
117 $ms %= MILLISECONDS_A_DAY

Callers

nothing calls this directly

Calls

no outgoing calls

Tested by

no test coverage detected