@cripty2001/utils 0.0.131 → 0.0.133

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,24 +166,32 @@ 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 = (seconds) => {
170
- console.log("getformat", seconds);
171
- if (seconds < 60)
172
- return "second";
173
- if (seconds < 3600)
174
- return "minute";
175
- if (seconds < 86400)
176
- return "hour";
177
- if (seconds < 604800)
178
- return "day";
179
- return "week";
169
+ const getFormat = (diff) => {
170
+ const breakpoints = [
171
+ { limit: 60, unit: "second" },
172
+ { limit: 3600, unit: "minute" },
173
+ { limit: 86400, unit: "hour" },
174
+ { limit: 604800, unit: "day" },
175
+ ];
176
+ for (const { limit, unit } of breakpoints) {
177
+ if (diff < limit)
178
+ return {
179
+ limit,
180
+ unit
181
+ };
182
+ }
183
+ return {
184
+ limit: 2592000,
185
+ unit: "week"
186
+ };
180
187
  };
181
188
  const cb = (0, react_1.useCallback)((ts) => {
182
189
  const now = currTs;
183
190
  const then = ts instanceof Date ? ts.getTime() : ts;
184
191
  const delta = then - now;
185
192
  const seconds = Math.round(delta / 1000);
186
- return rtf.format(seconds, getFormat(seconds));
193
+ const { limit, unit } = getFormat(seconds);
194
+ return rtf.format(Math.floor(seconds / limit), unit);
187
195
  }, [currTs, rtf]);
188
196
  return cb;
189
197
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@cripty2001/utils",
3
- "version": "0.0.131",
3
+ "version": "0.0.133",
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": {