@cripty2001/utils 0.0.134 → 0.0.136

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.
@@ -166,22 +166,39 @@ function useAsync(f, data, debouce = 200) {
166
166
  function useRelTime(refresh = 1000) {
167
167
  const currTs = useCurrentTimestamp(refresh);
168
168
  const rtf = (0, react_1.useRef)(new Intl.RelativeTimeFormat(navigator.language, { numeric: "auto" })).current;
169
- const getFormat = (diff) => {
169
+ const getFormat = (_diff) => {
170
+ const diff = Math.abs(_diff);
170
171
  const breakpoints = [
171
- { limit: 60, unit: "second" },
172
- { limit: 3600, unit: "minute" },
173
- { limit: 86400, unit: "hour" },
174
- { limit: 604800, unit: "day" },
172
+ {
173
+ base: 1,
174
+ limit: 60,
175
+ unit: "second"
176
+ },
177
+ {
178
+ base: 60,
179
+ limit: 60,
180
+ unit: "minute"
181
+ },
182
+ {
183
+ base: 60 * 60,
184
+ limit: 24,
185
+ unit: "hour"
186
+ },
187
+ {
188
+ base: 60 * 60 * 24,
189
+ limit: 45,
190
+ unit: "day"
191
+ },
175
192
  ];
176
- for (const { limit, unit } of breakpoints) {
177
- if (diff < limit)
193
+ for (const { base, limit, unit } of breakpoints) {
194
+ if (diff < limit * base)
178
195
  return {
179
- limit,
196
+ base,
180
197
  unit
181
198
  };
182
199
  }
183
200
  return {
184
- limit: 2592000,
201
+ base: 60 * 60 * 24 * 7,
185
202
  unit: "week"
186
203
  };
187
204
  };
@@ -190,9 +207,11 @@ function useRelTime(refresh = 1000) {
190
207
  const then = ts instanceof Date ? ts.getTime() : ts;
191
208
  const delta = then - now;
192
209
  const seconds = Math.round(delta / 1000);
193
- const { limit, unit } = getFormat(seconds);
194
- console.log("format", Math.floor(seconds / limit), unit);
195
- return rtf.format(Math.floor(seconds / limit), unit);
210
+ const { base, unit } = getFormat(seconds);
211
+ const rounded = seconds > 0 ?
212
+ Math.floor(seconds / base) :
213
+ Math.ceil(seconds / base);
214
+ return rtf.format(rounded, unit);
196
215
  }, [currTs, rtf]);
197
216
  return cb;
198
217
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@cripty2001/utils",
3
- "version": "0.0.134",
3
+ "version": "0.0.136",
4
4
  "description": "Internal Set of utils. If you need them use them, otherwise go to the next package ;)",
5
5
  "homepage": "https://github.com/cripty2001/utils#readme",
6
6
  "bugs": {