@vueuse/shared 10.4.0 → 10.5.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.
package/index.cjs CHANGED
@@ -3,12 +3,13 @@
3
3
  var vueDemi = require('vue-demi');
4
4
 
5
5
  function computedEager(fn, options) {
6
+ var _a;
6
7
  const result = vueDemi.shallowRef();
7
8
  vueDemi.watchEffect(() => {
8
9
  result.value = fn();
9
10
  }, {
10
11
  ...options,
11
- flush: options?.flush ?? "sync"
12
+ flush: (_a = options == null ? void 0 : options.flush) != null ? _a : "sync"
12
13
  });
13
14
  return vueDemi.readonly(result);
14
15
  }
@@ -38,7 +39,7 @@ function computedWithControl(source, fn) {
38
39
  return v;
39
40
  },
40
41
  set(v2) {
41
- set?.(v2);
42
+ set == null ? void 0 : set(v2);
42
43
  }
43
44
  };
44
45
  });
@@ -91,14 +92,39 @@ function createGlobalState(stateFactory) {
91
92
  };
92
93
  }
93
94
 
94
- function createInjectionState(composable) {
95
- const key = Symbol("InjectionState");
95
+ const localProvidedStateMap = /* @__PURE__ */ new WeakMap();
96
+
97
+ const provideLocal = (key, value) => {
98
+ var _a;
99
+ const instance = (_a = vueDemi.getCurrentInstance()) == null ? void 0 : _a.proxy;
100
+ if (instance == null)
101
+ throw new Error("provideLocal must be called in setup");
102
+ if (!localProvidedStateMap.has(instance))
103
+ localProvidedStateMap.set(instance, /* @__PURE__ */ Object.create(null));
104
+ const localProvidedState = localProvidedStateMap.get(instance);
105
+ localProvidedState[key] = value;
106
+ vueDemi.provide(key, value);
107
+ };
108
+
109
+ const injectLocal = (...args) => {
110
+ var _a;
111
+ const key = args[0];
112
+ const instance = (_a = vueDemi.getCurrentInstance()) == null ? void 0 : _a.proxy;
113
+ if (instance == null)
114
+ throw new Error("injectLocal must be called in setup");
115
+ if (localProvidedStateMap.has(instance) && key in localProvidedStateMap.get(instance))
116
+ return localProvidedStateMap.get(instance)[key];
117
+ return vueDemi.inject(...args);
118
+ };
119
+
120
+ function createInjectionState(composable, options) {
121
+ const key = (options == null ? void 0 : options.injectionKey) || Symbol("InjectionState");
96
122
  const useProvidingState = (...args) => {
97
123
  const state = composable(...args);
98
- vueDemi.provide(key, state);
124
+ provideLocal(key, state);
99
125
  return state;
100
126
  };
101
- const useInjectedState = () => vueDemi.inject(key);
127
+ const useInjectedState = () => injectLocal(key);
102
128
  return [useProvidingState, useInjectedState];
103
129
  }
104
130
 
@@ -188,7 +214,7 @@ function toValue(r) {
188
214
  const resolveUnref = toValue;
189
215
 
190
216
  function reactify(fn, options) {
191
- const unrefFn = options?.computedGetter === false ? vueDemi.unref : toValue;
217
+ const unrefFn = (options == null ? void 0 : options.computedGetter) === false ? vueDemi.unref : toValue;
192
218
  return function(...args) {
193
219
  return vueDemi.computed(() => fn.apply(this, args.map((i) => unrefFn(i))));
194
220
  };
@@ -257,9 +283,7 @@ function reactiveComputed(fn) {
257
283
  function reactiveOmit(obj, ...keys) {
258
284
  const flatKeys = keys.flat();
259
285
  const predicate = flatKeys[0];
260
- return reactiveComputed(
261
- () => typeof predicate === "function" ? Object.fromEntries(Object.entries(vueDemi.toRefs(obj)).filter(([k, v]) => !predicate(toValue(v), k))) : Object.fromEntries(Object.entries(vueDemi.toRefs(obj)).filter((e) => !flatKeys.includes(e[0])))
262
- );
286
+ return reactiveComputed(() => typeof predicate === "function" ? Object.fromEntries(Object.entries(vueDemi.toRefs(obj)).filter(([k, v]) => !predicate(toValue(v), k))) : Object.fromEntries(Object.entries(vueDemi.toRefs(obj)).filter((e) => !flatKeys.includes(e[0]))));
263
287
  }
264
288
 
265
289
  const isClient = typeof window !== "undefined" && typeof document !== "undefined";
@@ -284,7 +308,8 @@ const rand = (min, max) => {
284
308
  const hasOwn = (val, key) => Object.prototype.hasOwnProperty.call(val, key);
285
309
  const isIOS = /* @__PURE__ */ getIsIOS();
286
310
  function getIsIOS() {
287
- return isClient && window?.navigator?.userAgent && /* @__PURE__ */ /iP(ad|hone|od)/.test(window.navigator.userAgent);
311
+ var _a;
312
+ return isClient && ((_a = window == null ? void 0 : window.navigator) == null ? void 0 : _a.userAgent) && /* @__PURE__ */ /iP(ad|hone|od)/.test(window.navigator.userAgent);
288
313
  }
289
314
 
290
315
  function createFilterWrapper(filter, fn) {
@@ -414,9 +439,7 @@ function cacheStringFunction(fn) {
414
439
  };
415
440
  }
416
441
  const hyphenateRE = /\B([A-Z])/g;
417
- const hyphenate = cacheStringFunction(
418
- (str) => str.replace(hyphenateRE, "-$1").toLowerCase()
419
- );
442
+ const hyphenate = cacheStringFunction((str) => str.replace(hyphenateRE, "-$1").toLowerCase());
420
443
  const camelizeRE = /-(\w)/g;
421
444
  const camelize = cacheStringFunction((str) => {
422
445
  return str.replace(camelizeRE, (_, c) => c ? c.toUpperCase() : "");
@@ -455,9 +478,10 @@ function containsProp(obj, ...props) {
455
478
  return props.some((k) => k in obj);
456
479
  }
457
480
  function increaseWithUnit(target, delta) {
481
+ var _a;
458
482
  if (typeof target === "number")
459
483
  return target + delta;
460
- const value = target.match(/^-?[0-9]+\.?[0-9]*/)?.[0] || "";
484
+ const value = ((_a = target.match(/^-?[0-9]+\.?[0-9]*/)) == null ? void 0 : _a[0]) || "";
461
485
  const unit = target.slice(value.length);
462
486
  const result = Number.parseFloat(value) + delta;
463
487
  if (Number.isNaN(result))
@@ -541,7 +565,8 @@ function refDebounced(value, ms = 200, options = {}) {
541
565
  function refDefault(source, defaultValue) {
542
566
  return vueDemi.computed({
543
567
  get() {
544
- return source.value ?? defaultValue;
568
+ var _a;
569
+ return (_a = source.value) != null ? _a : defaultValue;
545
570
  },
546
571
  set(value) {
547
572
  source.value = value;
@@ -589,13 +614,14 @@ function refWithControl(initial, options = {}) {
589
614
  return source;
590
615
  }
591
616
  function set(value, triggering = true) {
617
+ var _a, _b;
592
618
  if (value === source)
593
619
  return;
594
620
  const old = source;
595
- if (options.onBeforeChange?.(value, old) === false)
621
+ if (((_a = options.onBeforeChange) == null ? void 0 : _a.call(options, value, old)) === false)
596
622
  return;
597
623
  source = value;
598
- options.onChanged?.(value, old);
624
+ (_b = options.onChanged) == null ? void 0 : _b.call(options, value, old);
599
625
  if (triggering)
600
626
  trigger();
601
627
  }
@@ -666,6 +692,7 @@ function watchPausable(source, cb, options = {}) {
666
692
  }
667
693
 
668
694
  function syncRef(left, right, options = {}) {
695
+ var _a, _b;
669
696
  const {
670
697
  flush = "sync",
671
698
  deep = false,
@@ -674,8 +701,8 @@ function syncRef(left, right, options = {}) {
674
701
  transform = {}
675
702
  } = options;
676
703
  const watchers = [];
677
- const transformLTR = transform.ltr ?? ((v) => v);
678
- const transformRTL = transform.rtl ?? ((v) => v);
704
+ const transformLTR = (_a = transform.ltr) != null ? _a : (v) => v;
705
+ const transformRTL = (_b = transform.rtl) != null ? _b : (v) => v;
679
706
  if (direction === "both" || direction === "ltr") {
680
707
  watchers.push(watchPausable(
681
708
  left,
@@ -729,7 +756,8 @@ function toRefs(objectRef, options = {}) {
729
756
  return objectRef.value[key];
730
757
  },
731
758
  set(v) {
732
- const replaceRef = toValue(options.replaceRef) ?? true;
759
+ var _a;
760
+ const replaceRef = (_a = toValue(options.replaceRef)) != null ? _a : true;
733
761
  if (replaceRef) {
734
762
  if (Array.isArray(objectRef.value)) {
735
763
  const copy = [...objectRef.value];
@@ -785,7 +813,7 @@ function createUntil(r, isNot = false) {
785
813
  r,
786
814
  (v) => {
787
815
  if (condition(v) !== isNot) {
788
- stop?.();
816
+ stop == null ? void 0 : stop();
789
817
  resolve(v);
790
818
  }
791
819
  },
@@ -799,7 +827,7 @@ function createUntil(r, isNot = false) {
799
827
  const promises = [watcher];
800
828
  if (timeout != null) {
801
829
  promises.push(
802
- promiseTimeout(timeout, throwOnTimeout).then(() => toValue(r)).finally(() => stop?.())
830
+ promiseTimeout(timeout, throwOnTimeout).then(() => toValue(r)).finally(() => stop == null ? void 0 : stop())
803
831
  );
804
832
  }
805
833
  return Promise.race(promises);
@@ -807,14 +835,14 @@ function createUntil(r, isNot = false) {
807
835
  function toBe(value, options) {
808
836
  if (!vueDemi.isRef(value))
809
837
  return toMatch((v) => v === value, options);
810
- const { flush = "sync", deep = false, timeout, throwOnTimeout } = options ?? {};
838
+ const { flush = "sync", deep = false, timeout, throwOnTimeout } = options != null ? options : {};
811
839
  let stop = null;
812
840
  const watcher = new Promise((resolve) => {
813
841
  stop = vueDemi.watch(
814
842
  [r, value],
815
843
  ([v1, v2]) => {
816
844
  if (isNot !== (v1 === v2)) {
817
- stop?.();
845
+ stop == null ? void 0 : stop();
818
846
  resolve(v1);
819
847
  }
820
848
  },
@@ -829,7 +857,7 @@ function createUntil(r, isNot = false) {
829
857
  if (timeout != null) {
830
858
  promises.push(
831
859
  promiseTimeout(timeout, throwOnTimeout).then(() => toValue(r)).finally(() => {
832
- stop?.();
860
+ stop == null ? void 0 : stop();
833
861
  return toValue(r);
834
862
  })
835
863
  );
@@ -900,9 +928,10 @@ function defaultComparator(value, othVal) {
900
928
  return value === othVal;
901
929
  }
902
930
  function useArrayDifference(...args) {
931
+ var _a;
903
932
  const list = args[0];
904
933
  const values = args[1];
905
- let compareFn = args[2] ?? defaultComparator;
934
+ let compareFn = (_a = args[2]) != null ? _a : defaultComparator;
906
935
  if (typeof compareFn === "string") {
907
936
  const key = compareFn;
908
937
  compareFn = (value, othVal) => value[key] === othVal[key];
@@ -919,11 +948,9 @@ function useArrayFilter(list, fn) {
919
948
  }
920
949
 
921
950
  function useArrayFind(list, fn) {
922
- return vueDemi.computed(
923
- () => toValue(
924
- toValue(list).find((element, index, array) => fn(toValue(element), index, array))
925
- )
926
- );
951
+ return vueDemi.computed(() => toValue(
952
+ toValue(list).find((element, index, array) => fn(toValue(element), index, array))
953
+ ));
927
954
  }
928
955
 
929
956
  function useArrayFindIndex(list, fn) {
@@ -939,35 +966,35 @@ function findLast(arr, cb) {
939
966
  return void 0;
940
967
  }
941
968
  function useArrayFindLast(list, fn) {
942
- return vueDemi.computed(
943
- () => toValue(
944
- !Array.prototype.findLast ? findLast(toValue(list), (element, index, array) => fn(toValue(element), index, array)) : toValue(list).findLast((element, index, array) => fn(toValue(element), index, array))
945
- )
946
- );
969
+ return vueDemi.computed(() => toValue(
970
+ !Array.prototype.findLast ? findLast(toValue(list), (element, index, array) => fn(toValue(element), index, array)) : toValue(list).findLast((element, index, array) => fn(toValue(element), index, array))
971
+ ));
947
972
  }
948
973
 
949
974
  function isArrayIncludesOptions(obj) {
950
975
  return isObject(obj) && containsProp(obj, "formIndex", "comparator");
951
976
  }
952
977
  function useArrayIncludes(...args) {
978
+ var _a;
953
979
  const list = args[0];
954
980
  const value = args[1];
955
981
  let comparator = args[2];
956
982
  let formIndex = 0;
957
983
  if (isArrayIncludesOptions(comparator)) {
958
- formIndex = comparator.fromIndex ?? 0;
984
+ formIndex = (_a = comparator.fromIndex) != null ? _a : 0;
959
985
  comparator = comparator.comparator;
960
986
  }
961
987
  if (typeof comparator === "string") {
962
988
  const key = comparator;
963
989
  comparator = (element, value2) => element[key] === toValue(value2);
964
990
  }
965
- comparator = comparator ?? ((element, value2) => element === toValue(value2));
966
- return vueDemi.computed(
967
- () => toValue(list).slice(formIndex).some(
968
- (element, index, array) => comparator(toValue(element), toValue(value), index, toValue(array))
969
- )
970
- );
991
+ comparator = comparator != null ? comparator : (element, value2) => element === toValue(value2);
992
+ return vueDemi.computed(() => toValue(list).slice(formIndex).some((element, index, array) => comparator(
993
+ toValue(element),
994
+ toValue(value),
995
+ index,
996
+ toValue(array)
997
+ )));
971
998
  }
972
999
 
973
1000
  function useArrayJoin(list, separator) {
@@ -1034,6 +1061,7 @@ function defaultMeridiem(hours, minutes, isLowercase, hasPeriod) {
1034
1061
  return isLowercase ? m.toLowerCase() : m;
1035
1062
  }
1036
1063
  function formatDate(date, formatStr, options = {}) {
1064
+ var _a;
1037
1065
  const years = date.getFullYear();
1038
1066
  const month = date.getMonth();
1039
1067
  const days = date.getDate();
@@ -1042,7 +1070,7 @@ function formatDate(date, formatStr, options = {}) {
1042
1070
  const seconds = date.getSeconds();
1043
1071
  const milliseconds = date.getMilliseconds();
1044
1072
  const day = date.getDay();
1045
- const meridiem = options.customMeridiem ?? defaultMeridiem;
1073
+ const meridiem = (_a = options.customMeridiem) != null ? _a : defaultMeridiem;
1046
1074
  const matches = {
1047
1075
  YY: () => String(years).slice(-2),
1048
1076
  YYYY: () => years,
@@ -1070,7 +1098,10 @@ function formatDate(date, formatStr, options = {}) {
1070
1098
  a: () => meridiem(hours, minutes, true),
1071
1099
  aa: () => meridiem(hours, minutes, true, true)
1072
1100
  };
1073
- return formatStr.replace(REGEX_FORMAT, (match, $1) => $1 ?? matches[match]?.() ?? match);
1101
+ return formatStr.replace(REGEX_FORMAT, (match, $1) => {
1102
+ var _a2, _b;
1103
+ return (_b = $1 != null ? $1 : (_a2 = matches[match]) == null ? void 0 : _a2.call(matches)) != null ? _b : match;
1104
+ });
1074
1105
  }
1075
1106
  function normalizeDate(date) {
1076
1107
  if (date === null)
@@ -1168,7 +1199,8 @@ function useInterval(interval = 1e3, options = {}) {
1168
1199
  }
1169
1200
 
1170
1201
  function useLastChanged(source, options = {}) {
1171
- const ms = vueDemi.ref(options.initialValue ?? null);
1202
+ var _a;
1203
+ const ms = vueDemi.ref((_a = options.initialValue) != null ? _a : null);
1172
1204
  vueDemi.watch(
1173
1205
  source,
1174
1206
  () => ms.value = timestamp(),
@@ -1221,7 +1253,7 @@ function useTimeout(interval = 1e3, options = {}) {
1221
1253
  callback
1222
1254
  } = options;
1223
1255
  const controls = useTimeoutFn(
1224
- callback ?? noop,
1256
+ callback != null ? callback : noop,
1225
1257
  interval,
1226
1258
  options
1227
1259
  );
@@ -1280,9 +1312,7 @@ function useToggle(initialValue = false, options = {}) {
1280
1312
  }
1281
1313
 
1282
1314
  function watchArray(source, cb, options) {
1283
- let oldList = options?.immediate ? [] : [
1284
- ...source instanceof Function ? source() : Array.isArray(source) ? source : toValue(source)
1285
- ];
1315
+ let oldList = (options == null ? void 0 : options.immediate) ? [] : [...source instanceof Function ? source() : Array.isArray(source) ? source : toValue(source)];
1286
1316
  return vueDemi.watch(source, (newList, _, onCleanup) => {
1287
1317
  const oldListRemains = Array.from({ length: oldList.length });
1288
1318
  const added = [];
@@ -1538,6 +1568,7 @@ exports.hyphenate = hyphenate;
1538
1568
  exports.identity = identity;
1539
1569
  exports.ignorableWatch = watchIgnorable;
1540
1570
  exports.increaseWithUnit = increaseWithUnit;
1571
+ exports.injectLocal = injectLocal;
1541
1572
  exports.invoke = invoke;
1542
1573
  exports.isClient = isClient;
1543
1574
  exports.isDef = isDef;
@@ -1555,6 +1586,7 @@ exports.objectPick = objectPick;
1555
1586
  exports.pausableFilter = pausableFilter;
1556
1587
  exports.pausableWatch = watchPausable;
1557
1588
  exports.promiseTimeout = promiseTimeout;
1589
+ exports.provideLocal = provideLocal;
1558
1590
  exports.rand = rand;
1559
1591
  exports.reactify = reactify;
1560
1592
  exports.reactifyObject = reactifyObject;