yummies 5.15.1 → 6.1.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.
Files changed (236) hide show
  1. package/README.md +10 -4
  2. package/async.cjs +45 -1
  3. package/async.cjs.map +1 -1
  4. package/async.d.ts +7 -6
  5. package/common.cjs +11 -1
  6. package/common.cjs.map +1 -1
  7. package/common.d.ts +8 -5
  8. package/common.js.map +1 -1
  9. package/complex.cjs +130 -1
  10. package/complex.cjs.map +1 -1
  11. package/complex.d.ts +113 -0
  12. package/complex.js +43 -1
  13. package/complex.js.map +1 -1
  14. package/cookie.cjs +9 -1
  15. package/cookie.cjs.map +1 -1
  16. package/cookie.d.ts +5 -3
  17. package/cookie.js.map +1 -1
  18. package/css.cjs +45 -1
  19. package/css.cjs.map +1 -1
  20. package/css.d.ts +11 -8
  21. package/css.js.map +1 -1
  22. package/data.cjs +43 -1
  23. package/data.cjs.map +1 -1
  24. package/data.d.ts +5 -4
  25. package/data.js.map +1 -1
  26. package/date-time.cjs +395 -1
  27. package/date-time.cjs.map +1 -1
  28. package/date-time.d.ts +12 -10
  29. package/date-time.js.map +1 -1
  30. package/device.cjs +32 -1
  31. package/device.cjs.map +1 -1
  32. package/device.d.ts +9 -8
  33. package/encodings.cjs +270 -1
  34. package/encodings.cjs.map +1 -1
  35. package/encodings.d.ts +3 -2
  36. package/encodings.js.map +1 -1
  37. package/errors.cjs +23 -1
  38. package/errors.cjs.map +1 -1
  39. package/errors.d.ts +5 -4
  40. package/errors.js.map +1 -1
  41. package/file.cjs +28 -1
  42. package/file.cjs.map +1 -1
  43. package/file.d.ts +4 -3
  44. package/format.cjs +75 -1
  45. package/format.cjs.map +1 -1
  46. package/format.d.ts +74 -0
  47. package/format.js.map +1 -1
  48. package/html.cjs +167 -1
  49. package/html.cjs.map +1 -1
  50. package/html.d.ts +19 -17
  51. package/html.js.map +1 -1
  52. package/id.cjs +25 -1
  53. package/id.cjs.map +1 -1
  54. package/id.d.ts +9 -8
  55. package/imports.cjs +33 -1
  56. package/imports.cjs.map +1 -1
  57. package/imports.d.ts +6 -4
  58. package/math.cjs +14 -1
  59. package/math.cjs.map +1 -1
  60. package/math.d.ts +7 -5
  61. package/math.js.map +1 -1
  62. package/media.cjs +105 -1
  63. package/media.cjs.map +1 -1
  64. package/media.d.ts +11 -11
  65. package/mobx.cjs +184 -1
  66. package/mobx.cjs.map +1 -1
  67. package/mobx.d.ts +66 -0
  68. package/mobx.js.map +1 -1
  69. package/ms.cjs +13 -1
  70. package/ms.cjs.map +1 -1
  71. package/ms.d.ts +4 -3
  72. package/number.cjs +10 -1
  73. package/number.cjs.map +1 -1
  74. package/number.d.ts +3 -2
  75. package/package.json +97 -141
  76. package/parser.cjs +68 -1
  77. package/parser.cjs.map +1 -1
  78. package/parser.d.ts +41 -0
  79. package/parser.js.map +1 -1
  80. package/price.cjs +20 -1
  81. package/price.cjs.map +1 -1
  82. package/price.d.ts +5 -3
  83. package/random.cjs +24 -1
  84. package/random.cjs.map +1 -1
  85. package/random.d.ts +10 -9
  86. package/react.cjs +216 -1
  87. package/react.cjs.map +1 -1
  88. package/react.d.ts +113 -0
  89. package/react.js.map +1 -1
  90. package/sound.cjs +11 -1
  91. package/sound.cjs.map +1 -1
  92. package/sound.d.ts +3 -2
  93. package/text.cjs +37 -1
  94. package/text.cjs.map +1 -1
  95. package/text.d.ts +4 -3
  96. package/type-guard.cjs +75 -1
  97. package/type-guard.cjs.map +1 -1
  98. package/type-guard.d.ts +121 -0
  99. package/type-guard.js.map +1 -1
  100. package/{utils/types.d.ts → types.d.ts} +54 -53
  101. package/types.global.cjs +2 -0
  102. package/types.global.cjs.map +1 -0
  103. package/{utility-types.d.ts → types.global.d.ts} +1 -1
  104. package/types.global.js +2 -0
  105. package/types.global.js.map +1 -0
  106. package/vibrate.cjs +8 -1
  107. package/vibrate.cjs.map +1 -1
  108. package/vibrate.d.ts +3 -2
  109. package/async.d.ts.map +0 -1
  110. package/common.d.ts.map +0 -1
  111. package/complex/counter.d.ts +0 -15
  112. package/complex/counter.d.ts.map +0 -1
  113. package/complex/global-config.d.ts +0 -11
  114. package/complex/global-config.d.ts.map +0 -1
  115. package/complex/index.d.ts +0 -4
  116. package/complex/index.d.ts.map +0 -1
  117. package/complex/modules-factory.d.ts +0 -49
  118. package/complex/modules-factory.d.ts.map +0 -1
  119. package/cookie.d.ts.map +0 -1
  120. package/css.d.ts.map +0 -1
  121. package/data.d.ts.map +0 -1
  122. package/date-time.d.ts.map +0 -1
  123. package/device.d.ts.map +0 -1
  124. package/encodings.d.ts.map +0 -1
  125. package/errors.d.ts.map +0 -1
  126. package/file.d.ts.map +0 -1
  127. package/format/_exports.d.ts +0 -5
  128. package/format/_exports.d.ts.map +0 -1
  129. package/format/constants.d.ts +0 -4
  130. package/format/constants.d.ts.map +0 -1
  131. package/format/index.d.ts +0 -3
  132. package/format/index.d.ts.map +0 -1
  133. package/format/number.d.ts +0 -36
  134. package/format/number.d.ts.map +0 -1
  135. package/format/percent.d.ts +0 -15
  136. package/format/percent.d.ts.map +0 -1
  137. package/format/skip-spaces.d.ts +0 -5
  138. package/format/skip-spaces.d.ts.map +0 -1
  139. package/html.d.ts.map +0 -1
  140. package/id.d.ts.map +0 -1
  141. package/imports.d.ts.map +0 -1
  142. package/math.d.ts.map +0 -1
  143. package/media.d.ts.map +0 -1
  144. package/mobx/apply-observable.d.ts +0 -4
  145. package/mobx/apply-observable.d.ts.map +0 -1
  146. package/mobx/create-enhanced-atom.d.ts +0 -11
  147. package/mobx/create-enhanced-atom.d.ts.map +0 -1
  148. package/mobx/create-ref.d.ts +0 -28
  149. package/mobx/create-ref.d.ts.map +0 -1
  150. package/mobx/deep-observable-struct.d.ts +0 -7
  151. package/mobx/deep-observable-struct.d.ts.map +0 -1
  152. package/mobx/get-mobx-administration.d.ts +0 -6
  153. package/mobx/get-mobx-administration.d.ts.map +0 -1
  154. package/mobx/index.d.ts +0 -7
  155. package/mobx/index.d.ts.map +0 -1
  156. package/mobx/lazy-observe.d.ts +0 -14
  157. package/mobx/lazy-observe.d.ts.map +0 -1
  158. package/ms.d.ts.map +0 -1
  159. package/number.d.ts.map +0 -1
  160. package/parser/_exports.d.ts +0 -4
  161. package/parser/_exports.d.ts.map +0 -1
  162. package/parser/index.d.ts +0 -3
  163. package/parser/index.d.ts.map +0 -1
  164. package/parser/number.d.ts +0 -21
  165. package/parser/number.d.ts.map +0 -1
  166. package/parser/percent.d.ts +0 -4
  167. package/parser/percent.d.ts.map +0 -1
  168. package/parser/string.d.ts +0 -7
  169. package/parser/string.d.ts.map +0 -1
  170. package/price.d.ts.map +0 -1
  171. package/random.d.ts.map +0 -1
  172. package/react/hooks/index.d.ts +0 -22
  173. package/react/hooks/index.d.ts.map +0 -1
  174. package/react/hooks/use-abort-controller.d.ts +0 -2
  175. package/react/hooks/use-abort-controller.d.ts.map +0 -1
  176. package/react/hooks/use-abort-signal.d.ts +0 -2
  177. package/react/hooks/use-abort-signal.d.ts.map +0 -1
  178. package/react/hooks/use-click-outside.d.ts +0 -9
  179. package/react/hooks/use-click-outside.d.ts.map +0 -1
  180. package/react/hooks/use-constant.d.ts +0 -9
  181. package/react/hooks/use-constant.d.ts.map +0 -1
  182. package/react/hooks/use-define-ref.d.ts +0 -10
  183. package/react/hooks/use-define-ref.d.ts.map +0 -1
  184. package/react/hooks/use-element-ref.d.ts +0 -2
  185. package/react/hooks/use-element-ref.d.ts.map +0 -1
  186. package/react/hooks/use-event-listener.d.ts +0 -8
  187. package/react/hooks/use-event-listener.d.ts.map +0 -1
  188. package/react/hooks/use-event.d.ts +0 -3
  189. package/react/hooks/use-event.d.ts.map +0 -1
  190. package/react/hooks/use-flag.d.ts +0 -8
  191. package/react/hooks/use-flag.d.ts.map +0 -1
  192. package/react/hooks/use-force-update.d.ts +0 -2
  193. package/react/hooks/use-force-update.d.ts.map +0 -1
  194. package/react/hooks/use-initial-height.d.ts +0 -5
  195. package/react/hooks/use-initial-height.d.ts.map +0 -1
  196. package/react/hooks/use-instance.d.ts +0 -27
  197. package/react/hooks/use-instance.d.ts.map +0 -1
  198. package/react/hooks/use-intersection-observer.d.ts +0 -2
  199. package/react/hooks/use-intersection-observer.d.ts.map +0 -1
  200. package/react/hooks/use-last-defined-value.d.ts +0 -2
  201. package/react/hooks/use-last-defined-value.d.ts.map +0 -1
  202. package/react/hooks/use-last-value-ref.d.ts +0 -2
  203. package/react/hooks/use-last-value-ref.d.ts.map +0 -1
  204. package/react/hooks/use-life-cycle.d.ts +0 -5
  205. package/react/hooks/use-life-cycle.d.ts.map +0 -1
  206. package/react/hooks/use-resize-observer.d.ts +0 -2
  207. package/react/hooks/use-resize-observer.d.ts.map +0 -1
  208. package/react/hooks/use-sync-ref.d.ts +0 -2
  209. package/react/hooks/use-sync-ref.d.ts.map +0 -1
  210. package/react/hooks/use-toggle.d.ts +0 -2
  211. package/react/hooks/use-toggle.d.ts.map +0 -1
  212. package/react/hooks/use-value.d.ts +0 -5
  213. package/react/hooks/use-value.d.ts.map +0 -1
  214. package/react/hooks/use-visibility-state.d.ts +0 -2
  215. package/react/hooks/use-visibility-state.d.ts.map +0 -1
  216. package/react/index.d.ts +0 -2
  217. package/react/index.d.ts.map +0 -1
  218. package/sound.d.ts.map +0 -1
  219. package/storage.d.ts +0 -39
  220. package/storage.d.ts.map +0 -1
  221. package/text.d.ts.map +0 -1
  222. package/type-guard/_exports.d.ts +0 -86
  223. package/type-guard/_exports.d.ts.map +0 -1
  224. package/type-guard/index.d.ts +0 -3
  225. package/type-guard/index.d.ts.map +0 -1
  226. package/utility-types.cjs +0 -2
  227. package/utility-types.cjs.map +0 -1
  228. package/utility-types.d.ts.map +0 -1
  229. package/utility-types.js +0 -2
  230. package/utility-types.js.map +0 -1
  231. package/utils/types.d.ts.map +0 -1
  232. package/vibrate.d.ts.map +0 -1
  233. /package/{utils/types.cjs → types.cjs} +0 -0
  234. /package/{utils/types.cjs.map → types.cjs.map} +0 -0
  235. /package/{utils/types.js → types.js} +0 -0
  236. /package/{utils/types.js.map → types.js.map} +0 -0
package/mobx.cjs CHANGED
@@ -1,2 +1,185 @@
1
- "use strict";Object.defineProperty(exports,Symbol.toStringTag,{value:"Module"});const e=require("mobx"),t=require("yummies/type-guard"),r=t=>{const r=t?.comparer??e.comparer.default,o=t=>{const s=t??null;r(o.current,s)||e.runInAction(()=>{o.current=s,o.listeners.forEach(e=>{e(o.current)})})};return o.listeners=new Set(t?.onChange?[t.onChange]:[]),(t?.onSet||t?.onUnset)&&o.listeners.add(e=>{e?t.onSet?.(e):t.onUnset?.()}),o.current=t?.initial??null,o.meta=t?.meta??{},e.makeObservable(o,{current:e.observable.ref,meta:e.observable}),o},o=e=>"function"==typeof e&&"current"in e;exports.DeepObservableStruct=class{data;constructor(t){this.data=t,e.makeObservable(this,{data:e.observable.deep,set:e.action})}set(e){const r=Object.keys(this.data).map(t=>[t,this.data,e]);let o=0,s=r.length;for(;o<s;){const[e,n,a]=r[o],c=a[e],i=n[e];if(o++,e in a)if(t.typeGuard.isObject(c)&&t.typeGuard.isObject(i)){const t=Object.keys(c);Object.keys(i).forEach(t=>{t in c||delete n[e][t]}),t.forEach(t=>{const o=r.push([t,n[e],c]);s=o})}else c!==i&&(n[e]=c);else delete n[e]}Object.keys(e).forEach(t=>{this.data[t]||(this.data[t]=e[t])})}},exports.applyObservable=(t,r,o)=>{o?(r.forEach(([e,r])=>{r(t,e)}),e.makeObservable(t)):e.makeObservable(t,Object.fromEntries(r))},exports.createEnhancedAtom=(t,r,o,s)=>{const n=e.createAtom(t,r&&(()=>r(n)),o&&(()=>o(n)));return n.meta=s??{},n.reportChanged=n.reportChanged.bind(n),n.reportObserved=n.reportObserved.bind(n),n},exports.createRef=r,exports.getMobxAdministration=t=>t[e.$mobx],exports.isRef=o,exports.lazyObserve=({context:t,property:r,onStart:o,onEnd:s,endDelay:n=!1})=>{let a,c;const i=new Set,d=Array.isArray(r)?r:[r],b=()=>{if(i.clear(),!1===n)return s?.(c,b),void(c=void 0);a&&(clearTimeout(a),a=void 0),a=setTimeout(()=>{s?.(c,b),a=void 0,c=void 0},n)},l=e=>{const t=i.size>0;i.add(e),t||(a&&(clearTimeout(a),a=void 0),c=o?.())},u=e=>{const t=!i.size;i.delete(e);const r=i.size>0;t||r||b()};return d.forEach(r=>{t?(e.onBecomeObserved(t,r,()=>l(r)),e.onBecomeUnobserved(t,r,()=>u(r))):(e.onBecomeObserved(r,()=>l(r)),e.onBecomeUnobserved(r,()=>u(r)))}),b},exports.toRef=(e,t)=>o(e)?e:r({initial:e,...t});
1
+ "use strict";
2
+ Object.defineProperty(exports, Symbol.toStringTag, { value: "Module" });
3
+ const mobx = require("mobx");
4
+ const typeGuard = require("yummies/type-guard");
5
+ const applyObservable = (context, annotationsArray, useDecorators) => {
6
+ if (useDecorators) {
7
+ annotationsArray.forEach(([field, annotation]) => {
8
+ annotation(context, field);
9
+ });
10
+ mobx.makeObservable(context);
11
+ } else {
12
+ mobx.makeObservable(context, Object.fromEntries(annotationsArray));
13
+ }
14
+ };
15
+ const createEnhancedAtom = (name, onBecomeObservedHandler, onBecomeUnobservedHandler, meta) => {
16
+ const atom = mobx.createAtom(
17
+ name,
18
+ onBecomeObservedHandler && (() => onBecomeObservedHandler(atom)),
19
+ onBecomeUnobservedHandler && (() => onBecomeUnobservedHandler(atom))
20
+ );
21
+ atom.meta = meta ?? {};
22
+ atom.reportChanged = atom.reportChanged.bind(atom);
23
+ atom.reportObserved = atom.reportObserved.bind(atom);
24
+ return atom;
25
+ };
26
+ const createRef = (cfg) => {
27
+ const comparer = cfg?.comparer ?? mobx.comparer.default;
28
+ const ref = (value) => {
29
+ const nextValue = value ?? null;
30
+ if (comparer(ref.current, nextValue)) {
31
+ return;
32
+ }
33
+ mobx.runInAction(() => {
34
+ ref.current = nextValue;
35
+ ref.listeners.forEach((listener) => {
36
+ listener(ref.current);
37
+ });
38
+ });
39
+ };
40
+ ref.listeners = new Set(cfg?.onChange ? [cfg.onChange] : []);
41
+ if (cfg?.onSet || cfg?.onUnset) {
42
+ ref.listeners.add((value) => {
43
+ if (value) {
44
+ cfg.onSet?.(value);
45
+ } else {
46
+ cfg.onUnset?.();
47
+ }
48
+ });
49
+ }
50
+ ref.current = cfg?.initial ?? null;
51
+ ref.meta = cfg?.meta ?? {};
52
+ mobx.makeObservable(ref, {
53
+ current: mobx.observable.ref,
54
+ meta: mobx.observable
55
+ });
56
+ return ref;
57
+ };
58
+ const isRef = (value) => {
59
+ return typeof value === "function" && "current" in value;
60
+ };
61
+ const toRef = (value, cfg) => {
62
+ return isRef(value) ? value : createRef({ initial: value, ...cfg });
63
+ };
64
+ class DeepObservableStruct {
65
+ data;
66
+ constructor(data) {
67
+ this.data = data;
68
+ mobx.makeObservable(this, {
69
+ data: mobx.observable.deep,
70
+ set: mobx.action
71
+ });
72
+ }
73
+ set(newData) {
74
+ const stack = Object.keys(this.data).map((key) => [
75
+ key,
76
+ this.data,
77
+ newData
78
+ ]);
79
+ let currentIndex = 0;
80
+ let stackLength = stack.length;
81
+ while (currentIndex < stackLength) {
82
+ const [key, currObservableData, newData2] = stack[currentIndex];
83
+ const newValue = newData2[key];
84
+ const currValue = currObservableData[key];
85
+ currentIndex++;
86
+ if (key in newData2) {
87
+ if (typeGuard.typeGuard.isObject(newValue) && typeGuard.typeGuard.isObject(currValue)) {
88
+ const newValueKeys = Object.keys(newValue);
89
+ Object.keys(currValue).forEach((childKey) => {
90
+ if (!(childKey in newValue)) {
91
+ delete currObservableData[key][childKey];
92
+ }
93
+ });
94
+ newValueKeys.forEach((childKey) => {
95
+ const length = stack.push([
96
+ childKey,
97
+ currObservableData[key],
98
+ newValue
99
+ ]);
100
+ stackLength = length;
101
+ });
102
+ } else if (newValue !== currValue) {
103
+ currObservableData[key] = newValue;
104
+ }
105
+ } else {
106
+ delete currObservableData[key];
107
+ }
108
+ }
109
+ Object.keys(newData).forEach((newDataKey) => {
110
+ if (!this.data[newDataKey]) {
111
+ this.data[newDataKey] = newData[newDataKey];
112
+ }
113
+ });
114
+ }
115
+ }
116
+ const getMobxAdministration = (context) => context[mobx.$mobx];
117
+ const lazyObserve = ({
118
+ context,
119
+ property,
120
+ onStart,
121
+ onEnd,
122
+ endDelay = false
123
+ }) => {
124
+ let timeoutId;
125
+ let metaData;
126
+ const observingProps = /* @__PURE__ */ new Set();
127
+ const properties = Array.isArray(property) ? property : [property];
128
+ const cleanup = () => {
129
+ observingProps.clear();
130
+ if (endDelay === false) {
131
+ onEnd?.(metaData, cleanup);
132
+ metaData = void 0;
133
+ return;
134
+ }
135
+ if (timeoutId) {
136
+ clearTimeout(timeoutId);
137
+ timeoutId = void 0;
138
+ }
139
+ timeoutId = setTimeout(() => {
140
+ onEnd?.(metaData, cleanup);
141
+ timeoutId = void 0;
142
+ metaData = void 0;
143
+ }, endDelay);
144
+ };
145
+ const start = (property2) => {
146
+ const isAlreadyObserving = observingProps.size > 0;
147
+ observingProps.add(property2);
148
+ if (isAlreadyObserving) {
149
+ return;
150
+ }
151
+ if (timeoutId) {
152
+ clearTimeout(timeoutId);
153
+ timeoutId = void 0;
154
+ }
155
+ metaData = onStart?.();
156
+ };
157
+ const stop = (property2) => {
158
+ const isAlreadyNotObserving = !observingProps.size;
159
+ observingProps.delete(property2);
160
+ const isObserving = observingProps.size > 0;
161
+ if (isAlreadyNotObserving || isObserving) {
162
+ return;
163
+ }
164
+ cleanup();
165
+ };
166
+ properties.forEach((property2) => {
167
+ if (context) {
168
+ mobx.onBecomeObserved(context, property2, () => start(property2));
169
+ mobx.onBecomeUnobserved(context, property2, () => stop(property2));
170
+ } else {
171
+ mobx.onBecomeObserved(property2, () => start(property2));
172
+ mobx.onBecomeUnobserved(property2, () => stop(property2));
173
+ }
174
+ });
175
+ return cleanup;
176
+ };
177
+ exports.DeepObservableStruct = DeepObservableStruct;
178
+ exports.applyObservable = applyObservable;
179
+ exports.createEnhancedAtom = createEnhancedAtom;
180
+ exports.createRef = createRef;
181
+ exports.getMobxAdministration = getMobxAdministration;
182
+ exports.isRef = isRef;
183
+ exports.lazyObserve = lazyObserve;
184
+ exports.toRef = toRef;
2
185
  //# sourceMappingURL=mobx.cjs.map
package/mobx.cjs.map CHANGED
@@ -1 +1 @@
1
- {"version":3,"file":"mobx.cjs","sources":["../src/mobx/create-ref.ts","../src/mobx/deep-observable-struct.ts","../src/mobx/apply-observable.ts","../src/mobx/create-enhanced-atom.ts","../src/mobx/get-mobx-administration.ts","../src/mobx/lazy-observe.ts"],"sourcesContent":["import {\n type IEqualsComparer,\n makeObservable,\n comparer as mobxComparer,\n observable,\n runInAction,\n} from 'mobx';\nimport type { AnyObject, Maybe } from 'yummies/utils/types';\n\nexport type RefChangeListener<T> = (value: T | null) => void;\n\n/**\n * Alternative to React.createRef but works in MobX world.\n * Typically it the should be the same React.LegacyRef (fn style)\n */\nexport interface Ref<T = any, TMeta = AnyObject> {\n (element: Maybe<T>): void;\n\n listeners: Set<RefChangeListener<NoInfer<T>>>;\n current: NoInfer<T> | null;\n meta: TMeta;\n}\n\nexport interface CreateRefConfig<T = any, TMeta = AnyObject> {\n onSet?: (node: T) => void;\n onUnset?: () => void;\n onChange?: RefChangeListener<T>;\n meta?: TMeta;\n initial?: Maybe<T>;\n comparer?: IEqualsComparer<T | null>;\n}\n\n/**\n * Creates ref thing to attach HTMLElements in React and all other\n */\nexport const createRef = <T = any, TMeta = AnyObject>(\n cfg?: CreateRefConfig<T, TMeta>,\n): Ref<T, TMeta> => {\n const comparer = cfg?.comparer ?? mobxComparer.default;\n\n const ref = ((value: Maybe<T>) => {\n const nextValue = value ?? null;\n\n if (comparer(ref.current, nextValue)) {\n return;\n }\n\n runInAction(() => {\n ref.current = nextValue;\n\n ref.listeners.forEach((listener) => {\n listener(ref.current);\n });\n });\n }) as Ref<T, TMeta>;\n\n ref.listeners = new Set(cfg?.onChange ? [cfg.onChange] : []);\n\n if (cfg?.onSet || cfg?.onUnset) {\n ref.listeners.add((value) => {\n if (value) {\n cfg.onSet?.(value);\n } else {\n cfg.onUnset?.();\n }\n });\n }\n\n ref.current = cfg?.initial ?? null;\n ref.meta = cfg?.meta ?? ({} as TMeta);\n\n makeObservable(ref, {\n current: observable.ref,\n meta: observable,\n });\n\n return ref;\n};\n\nexport const isRef = <T, TMeta = any>(\n value: T | Ref<T, TMeta>,\n): value is Ref<T, TMeta> => {\n return typeof value === 'function' && 'current' in value;\n};\n\nexport const toRef = <T, TMeta = any>(\n value: T | Ref<T, TMeta>,\n cfg?: Omit<CreateRefConfig<T, TMeta>, 'initial'>,\n): Ref<T, TMeta> => {\n return isRef(value) ? value : createRef({ initial: value, ...cfg });\n};\n","import { action, makeObservable, observable } from 'mobx';\nimport { typeGuard } from 'yummies/type-guard';\nimport type { AnyObject } from 'yummies/utils/types';\n\nexport class DeepObservableStruct<TData extends AnyObject> {\n data: TData;\n\n constructor(data: TData) {\n this.data = data;\n\n makeObservable(this, {\n data: observable.deep,\n set: action,\n });\n }\n\n set(newData: Partial<TData>) {\n type StackItem = [key: string, currObservable: AnyObject, new: AnyObject];\n\n const stack: StackItem[] = Object.keys(this.data).map((key) => [\n key,\n this.data,\n newData,\n ]);\n\n let currentIndex = 0;\n let stackLength = stack.length;\n\n while (currentIndex < stackLength) {\n const [key, currObservableData, newData] = stack[currentIndex];\n const newValue = newData[key];\n const currValue = currObservableData[key];\n\n currentIndex++;\n\n if (key in newData) {\n if (typeGuard.isObject(newValue) && typeGuard.isObject(currValue)) {\n const newValueKeys = Object.keys(newValue);\n\n Object.keys(currValue).forEach((childKey) => {\n if (!(childKey in newValue)) {\n delete currObservableData[key][childKey];\n }\n });\n\n newValueKeys.forEach((childKey) => {\n const length = stack.push([\n childKey,\n currObservableData[key],\n newValue,\n ]);\n stackLength = length;\n });\n } else if (newValue !== currValue) {\n currObservableData[key] = newValue;\n }\n } else {\n delete currObservableData[key];\n }\n }\n\n Object.keys(newData).forEach((newDataKey) => {\n if (!this.data[newDataKey]) {\n // @ts-expect-error\n this.data[newDataKey] = newData[newDataKey];\n }\n });\n }\n}\n","import { makeObservable } from 'mobx';\nimport type { AnyObject } from 'yummies/utils/types';\n\nexport type ObservableAnnotationsArray = [string, any][];\n\nexport const applyObservable = (\n context: AnyObject,\n annotationsArray: ObservableAnnotationsArray,\n useDecorators?: boolean,\n) => {\n if (useDecorators) {\n annotationsArray.forEach(([field, annotation]) => {\n annotation(context, field);\n });\n\n makeObservable(context);\n } else {\n makeObservable(context, Object.fromEntries(annotationsArray));\n }\n};\n","import { createAtom, type IAtom } from 'mobx';\nimport type { AnyObject } from 'yummies/utils/types';\n\nexport interface IEnhancedAtom<TMeta extends AnyObject = AnyObject>\n extends IAtom {\n meta: TMeta;\n}\n\n/**\n * Creates an enhanced atom with meta data\n * And bind `reportChanged` and `reportObserved` method to the atom\n */\nexport const createEnhancedAtom = <TMeta extends AnyObject>(\n name: string,\n onBecomeObservedHandler?: (atom: IEnhancedAtom<TMeta>) => void,\n onBecomeUnobservedHandler?: (atom: IEnhancedAtom<TMeta>) => void,\n meta?: TMeta,\n): IEnhancedAtom<TMeta> => {\n const atom = createAtom(\n name,\n onBecomeObservedHandler && (() => onBecomeObservedHandler(atom)),\n onBecomeUnobservedHandler && (() => onBecomeUnobservedHandler(atom)),\n ) as IEnhancedAtom<TMeta>;\n atom.meta = meta ?? ({} as TMeta);\n atom.reportChanged = atom.reportChanged.bind(atom);\n atom.reportObserved = atom.reportObserved.bind(atom);\n return atom;\n};\n","import { $mobx, type AnnotationMapEntry } from 'mobx';\nimport type { AnyObject } from 'yummies/utils/types';\n\ntype ObservableObjectAdministration = Parameters<\n Exclude<AnnotationMapEntry, boolean>['make_']\n>[0];\n\nexport const getMobxAdministration = (\n context: AnyObject,\n): ObservableObjectAdministration => context[$mobx];\n","import { onBecomeObserved, onBecomeUnobserved } from 'mobx';\n\n/**\n * When ONE OF the properties is becomes observed then `onStart` function is called.\n * WHen ALL properties are unobserved then `onEnd` function is called with the `metaData` that was returned by `onStart`.\n *\n * It uses `onBecomeObserved` and `onBecomeUnobserved` mobx hooks to perform lazy observation.\n */\nexport const lazyObserve = <TMetaData = void>({\n context,\n property,\n onStart,\n onEnd,\n endDelay = false,\n}: {\n context?: any;\n property: any | any[];\n onStart?: () => TMetaData;\n onEnd?: (metaData: TMetaData, cleanupFn: VoidFunction) => void;\n endDelay?: number | false;\n}) => {\n let timeoutId: ReturnType<typeof setTimeout> | undefined;\n let metaData: TMetaData | undefined;\n const observingProps = new Set<string>();\n const properties = Array.isArray(property) ? property : [property];\n\n const cleanup = () => {\n observingProps.clear();\n\n if (endDelay === false) {\n onEnd?.(metaData!, cleanup);\n metaData = undefined;\n return;\n }\n\n if (timeoutId) {\n clearTimeout(timeoutId);\n timeoutId = undefined;\n }\n\n timeoutId = setTimeout(() => {\n onEnd?.(metaData!, cleanup);\n timeoutId = undefined;\n metaData = undefined;\n }, endDelay);\n };\n\n const start = (property: string) => {\n const isAlreadyObserving = observingProps.size > 0;\n observingProps.add(property);\n\n if (isAlreadyObserving) {\n return;\n }\n\n if (timeoutId) {\n clearTimeout(timeoutId);\n timeoutId = undefined;\n }\n\n metaData = onStart?.();\n };\n\n const stop = (property: string) => {\n const isAlreadyNotObserving = !observingProps.size;\n\n observingProps.delete(property);\n\n const isObserving = observingProps.size > 0;\n\n if (isAlreadyNotObserving || isObserving) {\n return;\n }\n\n cleanup();\n };\n\n properties.forEach((property) => {\n if (context) {\n onBecomeObserved(context, property, () => start(property));\n onBecomeUnobserved(context, property, () => stop(property));\n } else {\n onBecomeObserved(property, () => start(property));\n onBecomeUnobserved(property, () => stop(property));\n }\n });\n\n return cleanup;\n};\n"],"names":["createRef","cfg","comparer","mobxComparer","default","ref","value","nextValue","current","runInAction","listeners","forEach","listener","Set","onChange","onSet","onUnset","add","initial","meta","makeObservable","observable","isRef","data","constructor","this","deep","set","action","newData","stack","Object","keys","map","key","currentIndex","stackLength","length","currObservableData","newValue","currValue","typeGuard","isObject","newValueKeys","childKey","push","newDataKey","context","annotationsArray","useDecorators","field","annotation","fromEntries","name","onBecomeObservedHandler","onBecomeUnobservedHandler","atom","createAtom","reportChanged","bind","reportObserved","$mobx","property","onStart","onEnd","endDelay","timeoutId","metaData","observingProps","properties","Array","isArray","cleanup","clear","clearTimeout","setTimeout","start","isAlreadyObserving","size","stop","isAlreadyNotObserving","delete","isObserving","onBecomeObserved","onBecomeUnobserved"],"mappings":"wIAmCaA,EACXC,IAEA,MAAMC,EAAWD,GAAKC,UAAYC,EAAAA,SAAaC,QAEzCC,EAAQC,IACZ,MAAMC,EAAYD,GAAS,KAEvBJ,EAASG,EAAIG,QAASD,IAI1BE,EAAAA,YAAY,KACVJ,EAAIG,QAAUD,EAEdF,EAAIK,UAAUC,QAASC,IACrBA,EAASP,EAAIG,cAyBnB,OApBAH,EAAIK,UAAY,IAAIG,IAAIZ,GAAKa,SAAW,CAACb,EAAIa,UAAY,KAErDb,GAAKc,OAASd,GAAKe,UACrBX,EAAIK,UAAUO,IAAKX,IACbA,EACFL,EAAIc,QAAQT,GAEZL,EAAIe,cAKVX,EAAIG,QAAUP,GAAKiB,SAAW,KAC9Bb,EAAIc,KAAOlB,GAAKkB,MAAS,CAAA,EAEzBC,EAAAA,eAAef,EAAK,CAClBG,QAASa,EAAAA,WAAWhB,IACpBc,KAAME,EAAAA,aAGDhB,GAGIiB,EACXhB,GAEwB,mBAAVA,GAAwB,YAAaA,+BC9E9C,MACLiB,KAEA,WAAAC,CAAYD,GACVE,KAAKF,KAAOA,EAEZH,EAAAA,eAAeK,KAAM,CACnBF,KAAMF,EAAAA,WAAWK,KACjBC,IAAKC,EAAAA,QAET,CAEA,GAAAD,CAAIE,GAGF,MAAMC,EAAqBC,OAAOC,KAAKP,KAAKF,MAAMU,IAAKC,GAAQ,CAC7DA,EACAT,KAAKF,KACLM,IAGF,IAAIM,EAAe,EACfC,EAAcN,EAAMO,OAExB,KAAOF,EAAeC,GAAa,CACjC,MAAOF,EAAKI,EAAoBT,GAAWC,EAAMK,GAC3CI,EAAWV,EAAQK,GACnBM,EAAYF,EAAmBJ,GAIrC,GAFAC,IAEID,K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WK,GACnBN,OAAY,EACZC,OAAW,GACVF,IAGCW,EAASd,IACb,MAAMe,EAAqBT,EAAeU,KAAO,EACjDV,EAAenD,IAAI6C,GAEfe,IAIAX,IACFQ,aAAaR,GACbA,OAAY,GAGdC,EAAWJ,QAGPgB,EAAQjB,IACZ,MAAMkB,GAAyBZ,EAAeU,KAE9CV,EAAea,OAAOnB,GAEtB,MAAMoB,EAAcd,EAAeU,KAAO,EAEtCE,GAAyBE,GAI7BV,KAaF,OAVAH,EAAW1D,QAASmD,IACdf,GACFoC,EAAAA,iBAAiBpC,EAASe,EAAU,IAAMc,EAAMd,IAChDsB,EAAAA,mBAAmBrC,EAASe,EAAU,IAAMiB,EAAKjB,MAEjDqB,EAAAA,iBAAiBrB,EAAU,IAAMc,EAAMd,IACvCsB,EAAAA,mBAAmBtB,EAAU,IAAMiB,EAAKjB,OAIrCU,iBLFY,CACnBlE,EACAL,IAEOqB,EAAMhB,GAASA,EAAQN,EAAU,CAAEkB,QAASZ,KAAUL"}
1
+ {"version":3,"file":"mobx.cjs","sources":["../src/mobx/apply-observable.ts","../src/mobx/create-enhanced-atom.ts","../src/mobx/create-ref.ts","../src/mobx/deep-observable-struct.ts","../src/mobx/get-mobx-administration.ts","../src/mobx/lazy-observe.ts"],"sourcesContent":["import { makeObservable } from 'mobx';\nimport type { AnyObject } from 'yummies/types';\n\nexport type ObservableAnnotationsArray = [string, any][];\n\nexport const applyObservable = (\n context: AnyObject,\n annotationsArray: ObservableAnnotationsArray,\n useDecorators?: boolean,\n) => {\n if (useDecorators) {\n annotationsArray.forEach(([field, annotation]) => {\n annotation(context, field);\n });\n\n makeObservable(context);\n } else {\n makeObservable(context, Object.fromEntries(annotationsArray));\n }\n};\n","import { createAtom, type IAtom } from 'mobx';\nimport type { AnyObject } from 'yummies/types';\n\nexport interface IEnhancedAtom<TMeta extends AnyObject = AnyObject>\n extends IAtom {\n meta: TMeta;\n}\n\n/**\n * Creates an enhanced atom with meta data\n * And bind `reportChanged` and `reportObserved` method to the atom\n */\nexport const createEnhancedAtom = <TMeta extends AnyObject>(\n name: string,\n onBecomeObservedHandler?: (atom: IEnhancedAtom<TMeta>) => void,\n onBecomeUnobservedHandler?: (atom: IEnhancedAtom<TMeta>) => void,\n meta?: TMeta,\n): IEnhancedAtom<TMeta> => {\n const atom = createAtom(\n name,\n onBecomeObservedHandler && (() => onBecomeObservedHandler(atom)),\n onBecomeUnobservedHandler && (() => onBecomeUnobservedHandler(atom)),\n ) as IEnhancedAtom<TMeta>;\n atom.meta = meta ?? ({} as TMeta);\n atom.reportChanged = atom.reportChanged.bind(atom);\n atom.reportObserved = atom.reportObserved.bind(atom);\n return atom;\n};\n","import {\n type IEqualsComparer,\n makeObservable,\n comparer as mobxComparer,\n observable,\n runInAction,\n} from 'mobx';\nimport type { AnyObject, Maybe } from 'yummies/types';\n\nexport type RefChangeListener<T> = (value: T | null) => void;\n\n/**\n * Alternative to React.createRef but works in MobX world.\n * Typically it the should be the same React.LegacyRef (fn style)\n */\nexport interface Ref<T = any, TMeta = AnyObject> {\n (element: Maybe<T>): void;\n\n listeners: Set<RefChangeListener<NoInfer<T>>>;\n current: NoInfer<T> | null;\n meta: TMeta;\n}\n\nexport interface CreateRefConfig<T = any, TMeta = AnyObject> {\n onSet?: (node: T) => void;\n onUnset?: () => void;\n onChange?: RefChangeListener<T>;\n meta?: TMeta;\n initial?: Maybe<T>;\n comparer?: IEqualsComparer<T | null>;\n}\n\n/**\n * Creates ref thing to attach HTMLElements in React and all other\n */\nexport const createRef = <T = any, TMeta = AnyObject>(\n cfg?: CreateRefConfig<T, TMeta>,\n): Ref<T, TMeta> => {\n const comparer = cfg?.comparer ?? mobxComparer.default;\n\n const ref = ((value: Maybe<T>) => {\n const nextValue = value ?? null;\n\n if (comparer(ref.current, nextValue)) {\n return;\n }\n\n runInAction(() => {\n ref.current = nextValue;\n\n ref.listeners.forEach((listener) => {\n listener(ref.current);\n });\n });\n }) as Ref<T, TMeta>;\n\n ref.listeners = new Set(cfg?.onChange ? [cfg.onChange] : []);\n\n if (cfg?.onSet || cfg?.onUnset) {\n ref.listeners.add((value) => {\n if (value) {\n cfg.onSet?.(value);\n } else {\n cfg.onUnset?.();\n }\n });\n }\n\n ref.current = cfg?.initial ?? null;\n ref.meta = cfg?.meta ?? ({} as TMeta);\n\n makeObservable(ref, {\n current: observable.ref,\n meta: observable,\n });\n\n return ref;\n};\n\nexport const isRef = <T, TMeta = any>(\n value: T | Ref<T, TMeta>,\n): value is Ref<T, TMeta> => {\n return typeof value === 'function' && 'current' in value;\n};\n\nexport const toRef = <T, TMeta = any>(\n value: T | Ref<T, TMeta>,\n cfg?: Omit<CreateRefConfig<T, TMeta>, 'initial'>,\n): Ref<T, TMeta> => {\n return isRef(value) ? value : createRef({ initial: value, ...cfg });\n};\n","import { action, makeObservable, observable } from 'mobx';\nimport { typeGuard } from 'yummies/type-guard';\nimport type { AnyObject } from 'yummies/types';\n\nexport class DeepObservableStruct<TData extends AnyObject> {\n data: TData;\n\n constructor(data: TData) {\n this.data = data;\n\n makeObservable(this, {\n data: observable.deep,\n set: action,\n });\n }\n\n set(newData: Partial<TData>) {\n type StackItem = [key: string, currObservable: AnyObject, new: AnyObject];\n\n const stack: StackItem[] = Object.keys(this.data).map((key) => [\n key,\n this.data,\n newData,\n ]);\n\n let currentIndex = 0;\n let stackLength = stack.length;\n\n while (currentIndex < stackLength) {\n const [key, currObservableData, newData] = stack[currentIndex];\n const newValue = newData[key];\n const currValue = currObservableData[key];\n\n currentIndex++;\n\n if (key in newData) {\n if (typeGuard.isObject(newValue) && typeGuard.isObject(currValue)) {\n const newValueKeys = Object.keys(newValue);\n\n Object.keys(currValue).forEach((childKey) => {\n if (!(childKey in newValue)) {\n delete currObservableData[key][childKey];\n }\n });\n\n newValueKeys.forEach((childKey) => {\n const length = stack.push([\n childKey,\n currObservableData[key],\n newValue,\n ]);\n stackLength = length;\n });\n } else if (newValue !== currValue) {\n currObservableData[key] = newValue;\n }\n } else {\n delete currObservableData[key];\n }\n }\n\n Object.keys(newData).forEach((newDataKey) => {\n if (!this.data[newDataKey]) {\n // @ts-expect-error\n this.data[newDataKey] = newData[newDataKey];\n }\n });\n }\n}\n","import { $mobx, type AnnotationMapEntry } from 'mobx';\nimport type { AnyObject } from 'yummies/types';\n\ntype ObservableObjectAdministration = Parameters<\n Exclude<AnnotationMapEntry, boolean>['make_']\n>[0];\n\nexport const getMobxAdministration = (\n context: AnyObject,\n): ObservableObjectAdministration => context[$mobx];\n","import { onBecomeObserved, onBecomeUnobserved } from 'mobx';\n\n/**\n * When ONE OF the properties is becomes observed then `onStart` function is called.\n * WHen ALL properties are unobserved then `onEnd` function is called with the `metaData` that was returned by `onStart`.\n *\n * It uses `onBecomeObserved` and `onBecomeUnobserved` mobx hooks to perform lazy observation.\n */\nexport const lazyObserve = <TMetaData = void>({\n context,\n property,\n onStart,\n onEnd,\n endDelay = false,\n}: {\n context?: any;\n property: any | any[];\n onStart?: () => TMetaData;\n onEnd?: (metaData: TMetaData, cleanupFn: VoidFunction) => void;\n endDelay?: number | false;\n}) => {\n let timeoutId: ReturnType<typeof setTimeout> | undefined;\n let metaData: TMetaData | undefined;\n const observingProps = new Set<string>();\n const properties = Array.isArray(property) ? property : [property];\n\n const cleanup = () => {\n observingProps.clear();\n\n if (endDelay === false) {\n onEnd?.(metaData!, cleanup);\n metaData = undefined;\n return;\n }\n\n if (timeoutId) {\n clearTimeout(timeoutId);\n timeoutId = undefined;\n }\n\n timeoutId = setTimeout(() => {\n onEnd?.(metaData!, cleanup);\n timeoutId = undefined;\n metaData = undefined;\n }, endDelay);\n };\n\n const start = (property: string) => {\n const isAlreadyObserving = observingProps.size > 0;\n observingProps.add(property);\n\n if (isAlreadyObserving) {\n return;\n }\n\n if (timeoutId) {\n clearTimeout(timeoutId);\n timeoutId = undefined;\n }\n\n metaData = onStart?.();\n };\n\n const stop = (property: string) => {\n const isAlreadyNotObserving = !observingProps.size;\n\n observingProps.delete(property);\n\n const isObserving = observingProps.size > 0;\n\n if (isAlreadyNotObserving || isObserving) {\n return;\n }\n\n cleanup();\n };\n\n properties.forEach((property) => {\n if (context) {\n onBecomeObserved(context, property, () => start(property));\n onBecomeUnobserved(context, property, () => stop(property));\n } else {\n onBecomeObserved(property, () => start(property));\n onBecomeUnobserved(property, () => stop(property));\n }\n });\n\n return cleanup;\n};\n"],"names":["makeObservable","createAtom","mobxComparer","runInAction","observable","action","newData","typeGuard","$mobx","property","onBecomeObserved","onBecomeUnobserved"],"mappings":";;;;AAKO,MAAM,kBAAkB,CAC7B,SACA,kBACA,kBACG;AACH,MAAI,eAAe;AACjB,qBAAiB,QAAQ,CAAC,CAAC,OAAO,UAAU,MAAM;AAChD,iBAAW,SAAS,KAAK;AAAA,IAC3B,CAAC;AAEDA,SAAAA,eAAe,OAAO;AAAA,EACxB,OAAO;AACLA,SAAAA,eAAe,SAAS,OAAO,YAAY,gBAAgB,CAAC;AAAA,EAC9D;AACF;ACPO,MAAM,qBAAqB,CAChC,MACA,yBACA,2BACA,SACyB;AACzB,QAAM,OAAOC,KAAAA;AAAAA,IACX;AAAA,IACA,4BAA4B,MAAM,wBAAwB,IAAI;AAAA,IAC9D,8BAA8B,MAAM,0BAA0B,IAAI;AAAA,EAAA;AAEpE,OAAK,OAAO,QAAS,CAAA;AACrB,OAAK,gBAAgB,KAAK,cAAc,KAAK,IAAI;AACjD,OAAK,iBAAiB,KAAK,eAAe,KAAK,IAAI;AACnD,SAAO;AACT;ACQO,MAAM,YAAY,CACvB,QACkB;AAClB,QAAM,WAAW,KAAK,YAAYC,KAAAA,SAAa;AAE/C,QAAM,MAAO,CAAC,UAAoB;AAChC,UAAM,YAAY,SAAS;AAE3B,QAAI,SAAS,IAAI,SAAS,SAAS,GAAG;AACpC;AAAA,IACF;AAEAC,SAAAA,YAAY,MAAM;AAChB,UAAI,UAAU;AAEd,UAAI,UAAU,QAAQ,CAAC,aAAa;AAClC,iBAAS,IAAI,OAAO;AAAA,MACtB,CAAC;AAAA,IACH,C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,UAAW,OAAO;AAC1B,kBAAY;AACZ,iBAAW;AAAA,IACb,GAAG,QAAQ;AAAA,EACb;AAEA,QAAM,QAAQ,CAACC,cAAqB;AAClC,UAAM,qBAAqB,eAAe,OAAO;AACjD,mBAAe,IAAIA,SAAQ;AAE3B,QAAI,oBAAoB;AACtB;AAAA,IACF;AAEA,QAAI,WAAW;AACb,mBAAa,SAAS;AACtB,kBAAY;AAAA,IACd;AAEA,eAAW,UAAA;AAAA,EACb;AAEA,QAAM,OAAO,CAACA,cAAqB;AACjC,UAAM,wBAAwB,CAAC,eAAe;AAE9C,mBAAe,OAAOA,SAAQ;AAE9B,UAAM,cAAc,eAAe,OAAO;AAE1C,QAAI,yBAAyB,aAAa;AACxC;AAAA,IACF;AAEA,YAAA;AAAA,EACF;AAEA,aAAW,QAAQ,CAACA,cAAa;AAC/B,QAAI,SAAS;AACXC,WAAAA,iBAAiB,SAASD,WAAU,MAAM,MAAMA,SAAQ,CAAC;AACzDE,WAAAA,mBAAmB,SAASF,WAAU,MAAM,KAAKA,SAAQ,CAAC;AAAA,IAC5D,OAAO;AACLC,WAAAA,iBAAiBD,WAAU,MAAM,MAAMA,SAAQ,CAAC;AAChDE,WAAAA,mBAAmBF,WAAU,MAAM,KAAKA,SAAQ,CAAC;AAAA,IACnD;AAAA,EACF,CAAC;AAED,SAAO;AACT;;;;;;;;;"}
package/mobx.d.ts ADDED
@@ -0,0 +1,66 @@
1
+ import { AnyObject, Maybe } from 'yummies/types';
2
+ import { IAtom, IEqualsComparer, AnnotationMapEntry } from 'mobx';
3
+
4
+ type ObservableAnnotationsArray = [string, any][];
5
+ declare const applyObservable: (context: AnyObject, annotationsArray: ObservableAnnotationsArray, useDecorators?: boolean) => void;
6
+
7
+ interface IEnhancedAtom<TMeta extends AnyObject = AnyObject> extends IAtom {
8
+ meta: TMeta;
9
+ }
10
+ /**
11
+ * Creates an enhanced atom with meta data
12
+ * And bind `reportChanged` and `reportObserved` method to the atom
13
+ */
14
+ declare const createEnhancedAtom: <TMeta extends AnyObject>(name: string, onBecomeObservedHandler?: (atom: IEnhancedAtom<TMeta>) => void, onBecomeUnobservedHandler?: (atom: IEnhancedAtom<TMeta>) => void, meta?: TMeta) => IEnhancedAtom<TMeta>;
15
+
16
+ type RefChangeListener<T> = (value: T | null) => void;
17
+ /**
18
+ * Alternative to React.createRef but works in MobX world.
19
+ * Typically it the should be the same React.LegacyRef (fn style)
20
+ */
21
+ interface Ref<T = any, TMeta = AnyObject> {
22
+ (element: Maybe<T>): void;
23
+ listeners: Set<RefChangeListener<NoInfer<T>>>;
24
+ current: NoInfer<T> | null;
25
+ meta: TMeta;
26
+ }
27
+ interface CreateRefConfig<T = any, TMeta = AnyObject> {
28
+ onSet?: (node: T) => void;
29
+ onUnset?: () => void;
30
+ onChange?: RefChangeListener<T>;
31
+ meta?: TMeta;
32
+ initial?: Maybe<T>;
33
+ comparer?: IEqualsComparer<T | null>;
34
+ }
35
+ /**
36
+ * Creates ref thing to attach HTMLElements in React and all other
37
+ */
38
+ declare const createRef: <T = any, TMeta = AnyObject>(cfg?: CreateRefConfig<T, TMeta>) => Ref<T, TMeta>;
39
+ declare const isRef: <T, TMeta = any>(value: T | Ref<T, TMeta>) => value is Ref<T, TMeta>;
40
+ declare const toRef: <T, TMeta = any>(value: T | Ref<T, TMeta>, cfg?: Omit<CreateRefConfig<T, TMeta>, "initial">) => Ref<T, TMeta>;
41
+
42
+ declare class DeepObservableStruct<TData extends AnyObject> {
43
+ data: TData;
44
+ constructor(data: TData);
45
+ set(newData: Partial<TData>): void;
46
+ }
47
+
48
+ type ObservableObjectAdministration = Parameters<Exclude<AnnotationMapEntry, boolean>['make_']>[0];
49
+ declare const getMobxAdministration: (context: AnyObject) => ObservableObjectAdministration;
50
+
51
+ /**
52
+ * When ONE OF the properties is becomes observed then `onStart` function is called.
53
+ * WHen ALL properties are unobserved then `onEnd` function is called with the `metaData` that was returned by `onStart`.
54
+ *
55
+ * It uses `onBecomeObserved` and `onBecomeUnobserved` mobx hooks to perform lazy observation.
56
+ */
57
+ declare const lazyObserve: <TMetaData = void>({ context, property, onStart, onEnd, endDelay, }: {
58
+ context?: any;
59
+ property: any | any[];
60
+ onStart?: () => TMetaData;
61
+ onEnd?: (metaData: TMetaData, cleanupFn: VoidFunction) => void;
62
+ endDelay?: number | false;
63
+ }) => () => void;
64
+
65
+ export { DeepObservableStruct, applyObservable, createEnhancedAtom, createRef, getMobxAdministration, isRef, lazyObserve, toRef };
66
+ export type { CreateRefConfig, IEnhancedAtom, ObservableAnnotationsArray, Ref, RefChangeListener };
package/mobx.js.map CHANGED
@@ -1 +1 @@
1
- {"version":3,"file":"mobx.js","sources":["../src/mobx/apply-observable.ts","../src/mobx/create-enhanced-atom.ts","../src/mobx/create-ref.ts","../src/mobx/deep-observable-struct.ts","../src/mobx/get-mobx-administration.ts","../src/mobx/lazy-observe.ts"],"sourcesContent":["import { makeObservable } from 'mobx';\nimport type { AnyObject } from 'yummies/utils/types';\n\nexport type ObservableAnnotationsArray = [string, any][];\n\nexport const applyObservable = (\n context: AnyObject,\n annotationsArray: ObservableAnnotationsArray,\n useDecorators?: boolean,\n) => {\n if (useDecorators) {\n annotationsArray.forEach(([field, annotation]) => {\n annotation(context, field);\n });\n\n makeObservable(context);\n } else {\n makeObservable(context, Object.fromEntries(annotationsArray));\n }\n};\n","import { createAtom, type IAtom } from 'mobx';\nimport type { AnyObject } from 'yummies/utils/types';\n\nexport interface IEnhancedAtom<TMeta extends AnyObject = AnyObject>\n extends IAtom {\n meta: TMeta;\n}\n\n/**\n * Creates an enhanced atom with meta data\n * And bind `reportChanged` and `reportObserved` method to the atom\n */\nexport const createEnhancedAtom = <TMeta extends AnyObject>(\n name: string,\n onBecomeObservedHandler?: (atom: IEnhancedAtom<TMeta>) => void,\n onBecomeUnobservedHandler?: (atom: IEnhancedAtom<TMeta>) => void,\n meta?: TMeta,\n): IEnhancedAtom<TMeta> => {\n const atom = createAtom(\n name,\n onBecomeObservedHandler && (() => onBecomeObservedHandler(atom)),\n onBecomeUnobservedHandler && (() => onBecomeUnobservedHandler(atom)),\n ) as IEnhancedAtom<TMeta>;\n atom.meta = meta ?? ({} as TMeta);\n atom.reportChanged = atom.reportChanged.bind(atom);\n atom.reportObserved = atom.reportObserved.bind(atom);\n return atom;\n};\n","import {\n type IEqualsComparer,\n makeObservable,\n comparer as mobxComparer,\n observable,\n runInAction,\n} from 'mobx';\nimport type { AnyObject, Maybe } from 'yummies/utils/types';\n\nexport type RefChangeListener<T> = (value: T | null) => void;\n\n/**\n * Alternative to React.createRef but works in MobX world.\n * Typically it the should be the same React.LegacyRef (fn style)\n */\nexport interface Ref<T = any, TMeta = AnyObject> {\n (element: Maybe<T>): void;\n\n listeners: Set<RefChangeListener<NoInfer<T>>>;\n current: NoInfer<T> | null;\n meta: TMeta;\n}\n\nexport interface CreateRefConfig<T = any, TMeta = AnyObject> {\n onSet?: (node: T) => void;\n onUnset?: () => void;\n onChange?: RefChangeListener<T>;\n meta?: TMeta;\n initial?: Maybe<T>;\n comparer?: IEqualsComparer<T | null>;\n}\n\n/**\n * Creates ref thing to attach HTMLElements in React and all other\n */\nexport const createRef = <T = any, TMeta = AnyObject>(\n cfg?: CreateRefConfig<T, TMeta>,\n): Ref<T, TMeta> => {\n const comparer = cfg?.comparer ?? mobxComparer.default;\n\n const ref = ((value: Maybe<T>) => {\n const nextValue = value ?? null;\n\n if (comparer(ref.current, nextValue)) {\n return;\n }\n\n runInAction(() => {\n ref.current = nextValue;\n\n ref.listeners.forEach((listener) => {\n listener(ref.current);\n });\n });\n }) as Ref<T, TMeta>;\n\n ref.listeners = new Set(cfg?.onChange ? [cfg.onChange] : []);\n\n if (cfg?.onSet || cfg?.onUnset) {\n ref.listeners.add((value) => {\n if (value) {\n cfg.onSet?.(value);\n } else {\n cfg.onUnset?.();\n }\n });\n }\n\n ref.current = cfg?.initial ?? null;\n ref.meta = cfg?.meta ?? ({} as TMeta);\n\n makeObservable(ref, {\n current: observable.ref,\n meta: observable,\n });\n\n return ref;\n};\n\nexport const isRef = <T, TMeta = any>(\n value: T | Ref<T, TMeta>,\n): value is Ref<T, TMeta> => {\n return typeof value === 'function' && 'current' in value;\n};\n\nexport const toRef = <T, TMeta = any>(\n value: T | Ref<T, TMeta>,\n cfg?: Omit<CreateRefConfig<T, TMeta>, 'initial'>,\n): Ref<T, TMeta> => {\n return isRef(value) ? value : createRef({ initial: value, ...cfg });\n};\n","import { action, makeObservable, observable } from 'mobx';\nimport { typeGuard } from 'yummies/type-guard';\nimport type { AnyObject } from 'yummies/utils/types';\n\nexport class DeepObservableStruct<TData extends AnyObject> {\n data: TData;\n\n constructor(data: TData) {\n this.data = data;\n\n makeObservable(this, {\n data: observable.deep,\n set: action,\n });\n }\n\n set(newData: Partial<TData>) {\n type StackItem = [key: string, currObservable: AnyObject, new: AnyObject];\n\n const stack: StackItem[] = Object.keys(this.data).map((key) => [\n key,\n this.data,\n newData,\n ]);\n\n let currentIndex = 0;\n let stackLength = stack.length;\n\n while (currentIndex < stackLength) {\n const [key, currObservableData, newData] = stack[currentIndex];\n const newValue = newData[key];\n const currValue = currObservableData[key];\n\n currentIndex++;\n\n if (key in newData) {\n if (typeGuard.isObject(newValue) && typeGuard.isObject(currValue)) {\n const newValueKeys = Object.keys(newValue);\n\n Object.keys(currValue).forEach((childKey) => {\n if (!(childKey in newValue)) {\n delete currObservableData[key][childKey];\n }\n });\n\n newValueKeys.forEach((childKey) => {\n const length = stack.push([\n childKey,\n currObservableData[key],\n newValue,\n ]);\n stackLength = length;\n });\n } else if (newValue !== currValue) {\n currObservableData[key] = newValue;\n }\n } else {\n delete currObservableData[key];\n }\n }\n\n Object.keys(newData).forEach((newDataKey) => {\n if (!this.data[newDataKey]) {\n // @ts-expect-error\n this.data[newDataKey] = newData[newDataKey];\n }\n });\n }\n}\n","import { $mobx, type AnnotationMapEntry } from 'mobx';\nimport type { AnyObject } from 'yummies/utils/types';\n\ntype ObservableObjectAdministration = Parameters<\n Exclude<AnnotationMapEntry, boolean>['make_']\n>[0];\n\nexport const getMobxAdministration = (\n context: AnyObject,\n): ObservableObjectAdministration => context[$mobx];\n","import { onBecomeObserved, onBecomeUnobserved } from 'mobx';\n\n/**\n * When ONE OF the properties is becomes observed then `onStart` function is called.\n * WHen ALL properties are unobserved then `onEnd` function is called with the `metaData` that was returned by `onStart`.\n *\n * It uses `onBecomeObserved` and `onBecomeUnobserved` mobx hooks to perform lazy observation.\n */\nexport const lazyObserve = <TMetaData = void>({\n context,\n property,\n onStart,\n onEnd,\n endDelay = false,\n}: {\n context?: any;\n property: any | any[];\n onStart?: () => TMetaData;\n onEnd?: (metaData: TMetaData, cleanupFn: VoidFunction) => void;\n endDelay?: number | false;\n}) => {\n let timeoutId: ReturnType<typeof setTimeout> | undefined;\n let metaData: TMetaData | undefined;\n const observingProps = new Set<string>();\n const properties = Array.isArray(property) ? property : [property];\n\n const cleanup = () => {\n observingProps.clear();\n\n if (endDelay === false) {\n onEnd?.(metaData!, cleanup);\n metaData = undefined;\n return;\n }\n\n if (timeoutId) {\n clearTimeout(timeoutId);\n timeoutId = undefined;\n }\n\n timeoutId = setTimeout(() => {\n onEnd?.(metaData!, cleanup);\n timeoutId = undefined;\n metaData = undefined;\n }, endDelay);\n };\n\n const start = (property: string) => {\n const isAlreadyObserving = observingProps.size > 0;\n observingProps.add(property);\n\n if (isAlreadyObserving) {\n return;\n }\n\n if (timeoutId) {\n clearTimeout(timeoutId);\n timeoutId = undefined;\n }\n\n metaData = onStart?.();\n };\n\n const stop = (property: string) => {\n const isAlreadyNotObserving = !observingProps.size;\n\n observingProps.delete(property);\n\n const isObserving = observingProps.size > 0;\n\n if (isAlreadyNotObserving || isObserving) {\n return;\n }\n\n cleanup();\n };\n\n properties.forEach((property) => {\n if (context) {\n onBecomeObserved(context, property, () => start(property));\n onBecomeUnobserved(context, property, () => stop(property));\n } else {\n onBecomeObserved(property, () => start(property));\n onBecomeUnobserved(property, () => stop(property));\n }\n });\n\n return cleanup;\n};\n"],"names":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,UAAM,cAAc,eAAe,OAAO;AAE1C,QAAI,yBAAyB,aAAa;AACxC;AAAA,IACF;AAEA,YAAA;AAAA,EACF;AAEA,aAAW,QAAQ,CAACA,cAAa;AAC/B,QAAI,SAAS;AACX,uBAAiB,SAASA,WAAU,MAAM,MAAMA,SAAQ,CAAC;AACzD,yBAAmB,SAASA,WAAU,MAAM,KAAKA,SAAQ,CAAC;AAAA,IAC5D,OAAO;AACL,uBAAiBA,WAAU,MAAM,MAAMA,SAAQ,CAAC;AAChD,yBAAmBA,WAAU,MAAM,KAAKA,SAAQ,CAAC;AAAA,IACnD;AAAA,EACF,CAAC;AAED,SAAO;AACT;"}
1
+ {"version":3,"file":"mobx.js","sources":["../src/mobx/apply-observable.ts","../src/mobx/create-enhanced-atom.ts","../src/mobx/create-ref.ts","../src/mobx/deep-observable-struct.ts","../src/mobx/get-mobx-administration.ts","../src/mobx/lazy-observe.ts"],"sourcesContent":["import { makeObservable } from 'mobx';\nimport type { AnyObject } from 'yummies/types';\n\nexport type ObservableAnnotationsArray = [string, any][];\n\nexport const applyObservable = (\n context: AnyObject,\n annotationsArray: ObservableAnnotationsArray,\n useDecorators?: boolean,\n) => {\n if (useDecorators) {\n annotationsArray.forEach(([field, annotation]) => {\n annotation(context, field);\n });\n\n makeObservable(context);\n } else {\n makeObservable(context, Object.fromEntries(annotationsArray));\n }\n};\n","import { createAtom, type IAtom } from 'mobx';\nimport type { AnyObject } from 'yummies/types';\n\nexport interface IEnhancedAtom<TMeta extends AnyObject = AnyObject>\n extends IAtom {\n meta: TMeta;\n}\n\n/**\n * Creates an enhanced atom with meta data\n * And bind `reportChanged` and `reportObserved` method to the atom\n */\nexport const createEnhancedAtom = <TMeta extends AnyObject>(\n name: string,\n onBecomeObservedHandler?: (atom: IEnhancedAtom<TMeta>) => void,\n onBecomeUnobservedHandler?: (atom: IEnhancedAtom<TMeta>) => void,\n meta?: TMeta,\n): IEnhancedAtom<TMeta> => {\n const atom = createAtom(\n name,\n onBecomeObservedHandler && (() => onBecomeObservedHandler(atom)),\n onBecomeUnobservedHandler && (() => onBecomeUnobservedHandler(atom)),\n ) as IEnhancedAtom<TMeta>;\n atom.meta = meta ?? ({} as TMeta);\n atom.reportChanged = atom.reportChanged.bind(atom);\n atom.reportObserved = atom.reportObserved.bind(atom);\n return atom;\n};\n","import {\n type IEqualsComparer,\n makeObservable,\n comparer as mobxComparer,\n observable,\n runInAction,\n} from 'mobx';\nimport type { AnyObject, Maybe } from 'yummies/types';\n\nexport type RefChangeListener<T> = (value: T | null) => void;\n\n/**\n * Alternative to React.createRef but works in MobX world.\n * Typically it the should be the same React.LegacyRef (fn style)\n */\nexport interface Ref<T = any, TMeta = AnyObject> {\n (element: Maybe<T>): void;\n\n listeners: Set<RefChangeListener<NoInfer<T>>>;\n current: NoInfer<T> | null;\n meta: TMeta;\n}\n\nexport interface CreateRefConfig<T = any, TMeta = AnyObject> {\n onSet?: (node: T) => void;\n onUnset?: () => void;\n onChange?: RefChangeListener<T>;\n meta?: TMeta;\n initial?: Maybe<T>;\n comparer?: IEqualsComparer<T | null>;\n}\n\n/**\n * Creates ref thing to attach HTMLElements in React and all other\n */\nexport const createRef = <T = any, TMeta = AnyObject>(\n cfg?: CreateRefConfig<T, TMeta>,\n): Ref<T, TMeta> => {\n const comparer = cfg?.comparer ?? mobxComparer.default;\n\n const ref = ((value: Maybe<T>) => {\n const nextValue = value ?? null;\n\n if (comparer(ref.current, nextValue)) {\n return;\n }\n\n runInAction(() => {\n ref.current = nextValue;\n\n ref.listeners.forEach((listener) => {\n listener(ref.current);\n });\n });\n }) as Ref<T, TMeta>;\n\n ref.listeners = new Set(cfg?.onChange ? [cfg.onChange] : []);\n\n if (cfg?.onSet || cfg?.onUnset) {\n ref.listeners.add((value) => {\n if (value) {\n cfg.onSet?.(value);\n } else {\n cfg.onUnset?.();\n }\n });\n }\n\n ref.current = cfg?.initial ?? null;\n ref.meta = cfg?.meta ?? ({} as TMeta);\n\n makeObservable(ref, {\n current: observable.ref,\n meta: observable,\n });\n\n return ref;\n};\n\nexport const isRef = <T, TMeta = any>(\n value: T | Ref<T, TMeta>,\n): value is Ref<T, TMeta> => {\n return typeof value === 'function' && 'current' in value;\n};\n\nexport const toRef = <T, TMeta = any>(\n value: T | Ref<T, TMeta>,\n cfg?: Omit<CreateRefConfig<T, TMeta>, 'initial'>,\n): Ref<T, TMeta> => {\n return isRef(value) ? value : createRef({ initial: value, ...cfg });\n};\n","import { action, makeObservable, observable } from 'mobx';\nimport { typeGuard } from 'yummies/type-guard';\nimport type { AnyObject } from 'yummies/types';\n\nexport class DeepObservableStruct<TData extends AnyObject> {\n data: TData;\n\n constructor(data: TData) {\n this.data = data;\n\n makeObservable(this, {\n data: observable.deep,\n set: action,\n });\n }\n\n set(newData: Partial<TData>) {\n type StackItem = [key: string, currObservable: AnyObject, new: AnyObject];\n\n const stack: StackItem[] = Object.keys(this.data).map((key) => [\n key,\n this.data,\n newData,\n ]);\n\n let currentIndex = 0;\n let stackLength = stack.length;\n\n while (currentIndex < stackLength) {\n const [key, currObservableData, newData] = stack[currentIndex];\n const newValue = newData[key];\n const currValue = currObservableData[key];\n\n currentIndex++;\n\n if (key in newData) {\n if (typeGuard.isObject(newValue) && typeGuard.isObject(currValue)) {\n const newValueKeys = Object.keys(newValue);\n\n Object.keys(currValue).forEach((childKey) => {\n if (!(childKey in newValue)) {\n delete currObservableData[key][childKey];\n }\n });\n\n newValueKeys.forEach((childKey) => {\n const length = stack.push([\n childKey,\n currObservableData[key],\n newValue,\n ]);\n stackLength = length;\n });\n } else if (newValue !== currValue) {\n currObservableData[key] = newValue;\n }\n } else {\n delete currObservableData[key];\n }\n }\n\n Object.keys(newData).forEach((newDataKey) => {\n if (!this.data[newDataKey]) {\n // @ts-expect-error\n this.data[newDataKey] = newData[newDataKey];\n }\n });\n }\n}\n","import { $mobx, type AnnotationMapEntry } from 'mobx';\nimport type { AnyObject } from 'yummies/types';\n\ntype ObservableObjectAdministration = Parameters<\n Exclude<AnnotationMapEntry, boolean>['make_']\n>[0];\n\nexport const getMobxAdministration = (\n context: AnyObject,\n): ObservableObjectAdministration => context[$mobx];\n","import { onBecomeObserved, onBecomeUnobserved } from 'mobx';\n\n/**\n * When ONE OF the properties is becomes observed then `onStart` function is called.\n * WHen ALL properties are unobserved then `onEnd` function is called with the `metaData` that was returned by `onStart`.\n *\n * It uses `onBecomeObserved` and `onBecomeUnobserved` mobx hooks to perform lazy observation.\n */\nexport const lazyObserve = <TMetaData = void>({\n context,\n property,\n onStart,\n onEnd,\n endDelay = false,\n}: {\n context?: any;\n property: any | any[];\n onStart?: () => TMetaData;\n onEnd?: (metaData: TMetaData, cleanupFn: VoidFunction) => void;\n endDelay?: number | false;\n}) => {\n let timeoutId: ReturnType<typeof setTimeout> | undefined;\n let metaData: TMetaData | undefined;\n const observingProps = new Set<string>();\n const properties = Array.isArray(property) ? property : [property];\n\n const cleanup = () => {\n observingProps.clear();\n\n if (endDelay === false) {\n onEnd?.(metaData!, cleanup);\n metaData = undefined;\n return;\n }\n\n if (timeoutId) {\n clearTimeout(timeoutId);\n timeoutId = undefined;\n }\n\n timeoutId = setTimeout(() => {\n onEnd?.(metaData!, cleanup);\n timeoutId = undefined;\n metaData = undefined;\n }, endDelay);\n };\n\n const start = (property: string) => {\n const isAlreadyObserving = observingProps.size > 0;\n observingProps.add(property);\n\n if (isAlreadyObserving) {\n return;\n }\n\n if (timeoutId) {\n clearTimeout(timeoutId);\n timeoutId = undefined;\n }\n\n metaData = onStart?.();\n };\n\n const stop = (property: string) => {\n const isAlreadyNotObserving = !observingProps.size;\n\n observingProps.delete(property);\n\n const isObserving = observingProps.size > 0;\n\n if (isAlreadyNotObserving || isObserving) {\n return;\n }\n\n cleanup();\n };\n\n properties.forEach((property) => {\n if (context) {\n onBecomeObserved(context, property, () => start(property));\n onBecomeUnobserved(context, property, () => stop(property));\n } else {\n onBecomeObserved(property, () => start(property));\n onBecomeUnobserved(property, () => stop(property));\n }\n });\n\n return cleanup;\n};\n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AAE9B,UAAM,cAAc,eAAe,OAAO;AAE1C,QAAI,yBAAyB,aAAa;AACxC;AAAA,IACF;AAEA,YAAA;AAAA,EACF;AAEA,aAAW,QAAQ,CAACA,cAAa;AAC/B,QAAI,SAAS;AACX,uBAAiB,SAASA,WAAU,MAAM,MAAMA,SAAQ,CAAC;AACzD,yBAAmB,SAASA,WAAU,MAAM,KAAKA,SAAQ,CAAC;AAAA,IAC5D,OAAO;AACL,uBAAiBA,WAAU,MAAM,MAAMA,SAAQ,CAAC;AAChD,yBAAmBA,WAAU,MAAM,KAAKA,SAAQ,CAAC;AAAA,IACnD;AAAA,EACF,CAAC;AAED,SAAO;AACT;"}
package/ms.cjs CHANGED
@@ -1,2 +1,14 @@
1
- "use strict";Object.defineProperty(exports,Symbol.toStringTag,{value:"Module"});const e={ms:1,sec:1e3,min:6e4,hour:36e5,day:864e5,week:6048e5};exports.ms=(s,t="ms")=>s*e[t],exports.unitsToMs=e;
1
+ "use strict";
2
+ Object.defineProperty(exports, Symbol.toStringTag, { value: "Module" });
3
+ const unitsToMs = {
4
+ ms: 1,
5
+ sec: 1e3,
6
+ min: 1e3 * 60,
7
+ hour: 1e3 * 60 * 60,
8
+ day: 1e3 * 60 * 60 * 24,
9
+ week: 1e3 * 60 * 60 * 24 * 7
10
+ };
11
+ const ms = (value, unit = "ms") => value * unitsToMs[unit];
12
+ exports.ms = ms;
13
+ exports.unitsToMs = unitsToMs;
2
14
  //# sourceMappingURL=ms.cjs.map
package/ms.cjs.map CHANGED
@@ -1 +1 @@
1
- {"version":3,"file":"ms.cjs","sources":["../src/ms.ts"],"sourcesContent":["export const unitsToMs = {\n ms: 1,\n sec: 1000,\n min: 1000 * 60,\n hour: 1000 * 60 * 60,\n day: 1000 * 60 * 60 * 24,\n week: 1000 * 60 * 60 * 24 * 7,\n} as const;\n\n/**\n * Переводит значение в юнитах в миллисекунды\n *\n * @example\n * ```ts\n * ms(1, 'min') // 60_000\n * ms(30, 'sec') // 30_000\n * ```\n */\nexport const ms = (value: number, unit: keyof typeof unitsToMs = 'ms') =>\n value * unitsToMs[unit];\n"],"names":["unitsToMs","ms","sec","min","hour","day","week","value","unit"],"mappings":"gFAAO,MAAMA,EAAY,CACvBC,GAAI,EACJC,IAAK,IACLC,IAAK,IACLC,KAAM,KACNC,IAAK,MACLC,KAAM,mBAYU,CAACC,EAAeC,EAA+B,OAC/DD,EAAQP,EAAUQ"}
1
+ {"version":3,"file":"ms.cjs","sources":["../src/ms.ts"],"sourcesContent":["export const unitsToMs = {\n ms: 1,\n sec: 1000,\n min: 1000 * 60,\n hour: 1000 * 60 * 60,\n day: 1000 * 60 * 60 * 24,\n week: 1000 * 60 * 60 * 24 * 7,\n} as const;\n\n/**\n * Переводит значение в юнитах в миллисекунды\n *\n * @example\n * ```ts\n * ms(1, 'min') // 60_000\n * ms(30, 'sec') // 30_000\n * ```\n */\nexport const ms = (value: number, unit: keyof typeof unitsToMs = 'ms') =>\n value * unitsToMs[unit];\n"],"names":[],"mappings":";;AAAO,MAAM,YAAY;AAAA,EACvB,IAAI;AAAA,EACJ,KAAK;AAAA,EACL,KAAK,MAAO;AAAA,EACZ,MAAM,MAAO,KAAK;AAAA,EAClB,KAAK,MAAO,KAAK,KAAK;AAAA,EACtB,MAAM,MAAO,KAAK,KAAK,KAAK;AAC9B;AAWO,MAAM,KAAK,CAAC,OAAe,OAA+B,SAC/D,QAAQ,UAAU,IAAI;;;"}
package/ms.d.ts CHANGED
@@ -1,4 +1,4 @@
1
- export declare const unitsToMs: {
1
+ declare const unitsToMs: {
2
2
  readonly ms: 1;
3
3
  readonly sec: 1000;
4
4
  readonly min: number;
@@ -15,5 +15,6 @@ export declare const unitsToMs: {
15
15
  * ms(30, 'sec') // 30_000
16
16
  * ```
17
17
  */
18
- export declare const ms: (value: number, unit?: keyof typeof unitsToMs) => number;
19
- //# sourceMappingURL=ms.d.ts.map
18
+ declare const ms: (value: number, unit?: keyof typeof unitsToMs) => number;
19
+
20
+ export { ms, unitsToMs };
package/number.cjs CHANGED
@@ -1,2 +1,11 @@
1
- "use strict";Object.defineProperty(exports,Symbol.toStringTag,{value:"Module"}),exports.round=function(t,r=0){if(!r)return Math.round(t);const e=10**r;return Math.round(t*e)/e};
1
+ "use strict";
2
+ Object.defineProperty(exports, Symbol.toStringTag, { value: "Module" });
3
+ function round(value, decimalPlaces = 0) {
4
+ if (!decimalPlaces) {
5
+ return Math.round(value);
6
+ }
7
+ const factor = 10 ** decimalPlaces;
8
+ return Math.round(value * factor) / factor;
9
+ }
10
+ exports.round = round;
2
11
  //# sourceMappingURL=number.cjs.map
package/number.cjs.map CHANGED
@@ -1 +1 @@
1
- {"version":3,"file":"number.cjs","sources":["../src/number.ts"],"sourcesContent":["/**\n * Works like `parseFloat(number.toFixed(4))` but performance better\n *\n * @example\n * round(191.212999999999999999999999, 4) // 191.213\n */\nexport function round(value: number, decimalPlaces: number = 0): number {\n if (!decimalPlaces) {\n return Math.round(value);\n }\n\n const factor = 10 ** decimalPlaces;\n return Math.round(value * factor) / factor;\n}\n"],"names":["value","decimalPlaces","Math","round","factor"],"mappings":"8FAMO,SAAeA,EAAeC,EAAwB,GAC3D,IAAKA,EACH,OAAOC,KAAKC,MAAMH,GAGpB,MAAMI,EAAS,IAAMH,EACrB,OAAOC,KAAKC,MAAMH,EAAQI,GAAUA,CACtC"}
1
+ {"version":3,"file":"number.cjs","sources":["../src/number.ts"],"sourcesContent":["/**\n * Works like `parseFloat(number.toFixed(4))` but performance better\n *\n * @example\n * round(191.212999999999999999999999, 4) // 191.213\n */\nexport function round(value: number, decimalPlaces: number = 0): number {\n if (!decimalPlaces) {\n return Math.round(value);\n }\n\n const factor = 10 ** decimalPlaces;\n return Math.round(value * factor) / factor;\n}\n"],"names":[],"mappings":";;AAMO,SAAS,MAAM,OAAe,gBAAwB,GAAW;AACtE,MAAI,CAAC,eAAe;AAClB,WAAO,KAAK,MAAM,KAAK;AAAA,EACzB;AAEA,QAAM,SAAS,MAAM;AACrB,SAAO,KAAK,MAAM,QAAQ,MAAM,IAAI;AACtC;;"}
package/number.d.ts CHANGED
@@ -4,5 +4,6 @@
4
4
  * @example
5
5
  * round(191.212999999999999999999999, 4) // 191.213
6
6
  */
7
- export declare function round(value: number, decimalPlaces?: number): number;
8
- //# sourceMappingURL=number.d.ts.map
7
+ declare function round(value: number, decimalPlaces?: number): number;
8
+
9
+ export { round };