@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.mjs CHANGED
@@ -1,12 +1,13 @@
1
- import { shallowRef, watchEffect, readonly, ref, watch, customRef, getCurrentScope, onScopeDispose, effectScope, provide, inject, isVue3, version, isRef, unref, computed, reactive, toRefs as toRefs$1, toRef as toRef$1, isVue2, set as set$1, getCurrentInstance, onBeforeMount, nextTick, onBeforeUnmount, onMounted, onUnmounted, isReactive } from 'vue-demi';
1
+ import { shallowRef, watchEffect, readonly, ref, watch, customRef, getCurrentScope, onScopeDispose, effectScope, getCurrentInstance, provide, inject, isVue3, version, isRef, unref, computed, reactive, toRefs as toRefs$1, toRef as toRef$1, isVue2, set as set$1, onBeforeMount, nextTick, onBeforeUnmount, onMounted, onUnmounted, isReactive } from 'vue-demi';
2
2
 
3
3
  function computedEager(fn, options) {
4
+ var _a;
4
5
  const result = shallowRef();
5
6
  watchEffect(() => {
6
7
  result.value = fn();
7
8
  }, {
8
9
  ...options,
9
- flush: options?.flush ?? "sync"
10
+ flush: (_a = options == null ? void 0 : options.flush) != null ? _a : "sync"
10
11
  });
11
12
  return readonly(result);
12
13
  }
@@ -36,7 +37,7 @@ function computedWithControl(source, fn) {
36
37
  return v;
37
38
  },
38
39
  set(v2) {
39
- set?.(v2);
40
+ set == null ? void 0 : set(v2);
40
41
  }
41
42
  };
42
43
  });
@@ -89,14 +90,39 @@ function createGlobalState(stateFactory) {
89
90
  };
90
91
  }
91
92
 
92
- function createInjectionState(composable) {
93
- const key = Symbol("InjectionState");
93
+ const localProvidedStateMap = /* @__PURE__ */ new WeakMap();
94
+
95
+ const provideLocal = (key, value) => {
96
+ var _a;
97
+ const instance = (_a = getCurrentInstance()) == null ? void 0 : _a.proxy;
98
+ if (instance == null)
99
+ throw new Error("provideLocal must be called in setup");
100
+ if (!localProvidedStateMap.has(instance))
101
+ localProvidedStateMap.set(instance, /* @__PURE__ */ Object.create(null));
102
+ const localProvidedState = localProvidedStateMap.get(instance);
103
+ localProvidedState[key] = value;
104
+ provide(key, value);
105
+ };
106
+
107
+ const injectLocal = (...args) => {
108
+ var _a;
109
+ const key = args[0];
110
+ const instance = (_a = getCurrentInstance()) == null ? void 0 : _a.proxy;
111
+ if (instance == null)
112
+ throw new Error("injectLocal must be called in setup");
113
+ if (localProvidedStateMap.has(instance) && key in localProvidedStateMap.get(instance))
114
+ return localProvidedStateMap.get(instance)[key];
115
+ return inject(...args);
116
+ };
117
+
118
+ function createInjectionState(composable, options) {
119
+ const key = (options == null ? void 0 : options.injectionKey) || Symbol("InjectionState");
94
120
  const useProvidingState = (...args) => {
95
121
  const state = composable(...args);
96
- provide(key, state);
122
+ provideLocal(key, state);
97
123
  return state;
98
124
  };
99
- const useInjectedState = () => inject(key);
125
+ const useInjectedState = () => injectLocal(key);
100
126
  return [useProvidingState, useInjectedState];
101
127
  }
102
128
 
@@ -186,7 +212,7 @@ function toValue(r) {
186
212
  const resolveUnref = toValue;
187
213
 
188
214
  function reactify(fn, options) {
189
- const unrefFn = options?.computedGetter === false ? unref : toValue;
215
+ const unrefFn = (options == null ? void 0 : options.computedGetter) === false ? unref : toValue;
190
216
  return function(...args) {
191
217
  return computed(() => fn.apply(this, args.map((i) => unrefFn(i))));
192
218
  };
@@ -255,9 +281,7 @@ function reactiveComputed(fn) {
255
281
  function reactiveOmit(obj, ...keys) {
256
282
  const flatKeys = keys.flat();
257
283
  const predicate = flatKeys[0];
258
- return reactiveComputed(
259
- () => typeof predicate === "function" ? Object.fromEntries(Object.entries(toRefs$1(obj)).filter(([k, v]) => !predicate(toValue(v), k))) : Object.fromEntries(Object.entries(toRefs$1(obj)).filter((e) => !flatKeys.includes(e[0])))
260
- );
284
+ return reactiveComputed(() => typeof predicate === "function" ? Object.fromEntries(Object.entries(toRefs$1(obj)).filter(([k, v]) => !predicate(toValue(v), k))) : Object.fromEntries(Object.entries(toRefs$1(obj)).filter((e) => !flatKeys.includes(e[0]))));
261
285
  }
262
286
 
263
287
  const isClient = typeof window !== "undefined" && typeof document !== "undefined";
@@ -282,7 +306,8 @@ const rand = (min, max) => {
282
306
  const hasOwn = (val, key) => Object.prototype.hasOwnProperty.call(val, key);
283
307
  const isIOS = /* @__PURE__ */ getIsIOS();
284
308
  function getIsIOS() {
285
- return isClient && window?.navigator?.userAgent && /* @__PURE__ */ /iP(ad|hone|od)/.test(window.navigator.userAgent);
309
+ var _a;
310
+ return isClient && ((_a = window == null ? void 0 : window.navigator) == null ? void 0 : _a.userAgent) && /* @__PURE__ */ /iP(ad|hone|od)/.test(window.navigator.userAgent);
286
311
  }
287
312
 
288
313
  function createFilterWrapper(filter, fn) {
@@ -412,9 +437,7 @@ function cacheStringFunction(fn) {
412
437
  };
413
438
  }
414
439
  const hyphenateRE = /\B([A-Z])/g;
415
- const hyphenate = cacheStringFunction(
416
- (str) => str.replace(hyphenateRE, "-$1").toLowerCase()
417
- );
440
+ const hyphenate = cacheStringFunction((str) => str.replace(hyphenateRE, "-$1").toLowerCase());
418
441
  const camelizeRE = /-(\w)/g;
419
442
  const camelize = cacheStringFunction((str) => {
420
443
  return str.replace(camelizeRE, (_, c) => c ? c.toUpperCase() : "");
@@ -453,9 +476,10 @@ function containsProp(obj, ...props) {
453
476
  return props.some((k) => k in obj);
454
477
  }
455
478
  function increaseWithUnit(target, delta) {
479
+ var _a;
456
480
  if (typeof target === "number")
457
481
  return target + delta;
458
- const value = target.match(/^-?[0-9]+\.?[0-9]*/)?.[0] || "";
482
+ const value = ((_a = target.match(/^-?[0-9]+\.?[0-9]*/)) == null ? void 0 : _a[0]) || "";
459
483
  const unit = target.slice(value.length);
460
484
  const result = Number.parseFloat(value) + delta;
461
485
  if (Number.isNaN(result))
@@ -539,7 +563,8 @@ function refDebounced(value, ms = 200, options = {}) {
539
563
  function refDefault(source, defaultValue) {
540
564
  return computed({
541
565
  get() {
542
- return source.value ?? defaultValue;
566
+ var _a;
567
+ return (_a = source.value) != null ? _a : defaultValue;
543
568
  },
544
569
  set(value) {
545
570
  source.value = value;
@@ -587,13 +612,14 @@ function refWithControl(initial, options = {}) {
587
612
  return source;
588
613
  }
589
614
  function set(value, triggering = true) {
615
+ var _a, _b;
590
616
  if (value === source)
591
617
  return;
592
618
  const old = source;
593
- if (options.onBeforeChange?.(value, old) === false)
619
+ if (((_a = options.onBeforeChange) == null ? void 0 : _a.call(options, value, old)) === false)
594
620
  return;
595
621
  source = value;
596
- options.onChanged?.(value, old);
622
+ (_b = options.onChanged) == null ? void 0 : _b.call(options, value, old);
597
623
  if (triggering)
598
624
  trigger();
599
625
  }
@@ -664,6 +690,7 @@ function watchPausable(source, cb, options = {}) {
664
690
  }
665
691
 
666
692
  function syncRef(left, right, options = {}) {
693
+ var _a, _b;
667
694
  const {
668
695
  flush = "sync",
669
696
  deep = false,
@@ -672,8 +699,8 @@ function syncRef(left, right, options = {}) {
672
699
  transform = {}
673
700
  } = options;
674
701
  const watchers = [];
675
- const transformLTR = transform.ltr ?? ((v) => v);
676
- const transformRTL = transform.rtl ?? ((v) => v);
702
+ const transformLTR = (_a = transform.ltr) != null ? _a : (v) => v;
703
+ const transformRTL = (_b = transform.rtl) != null ? _b : (v) => v;
677
704
  if (direction === "both" || direction === "ltr") {
678
705
  watchers.push(watchPausable(
679
706
  left,
@@ -727,7 +754,8 @@ function toRefs(objectRef, options = {}) {
727
754
  return objectRef.value[key];
728
755
  },
729
756
  set(v) {
730
- const replaceRef = toValue(options.replaceRef) ?? true;
757
+ var _a;
758
+ const replaceRef = (_a = toValue(options.replaceRef)) != null ? _a : true;
731
759
  if (replaceRef) {
732
760
  if (Array.isArray(objectRef.value)) {
733
761
  const copy = [...objectRef.value];
@@ -783,7 +811,7 @@ function createUntil(r, isNot = false) {
783
811
  r,
784
812
  (v) => {
785
813
  if (condition(v) !== isNot) {
786
- stop?.();
814
+ stop == null ? void 0 : stop();
787
815
  resolve(v);
788
816
  }
789
817
  },
@@ -797,7 +825,7 @@ function createUntil(r, isNot = false) {
797
825
  const promises = [watcher];
798
826
  if (timeout != null) {
799
827
  promises.push(
800
- promiseTimeout(timeout, throwOnTimeout).then(() => toValue(r)).finally(() => stop?.())
828
+ promiseTimeout(timeout, throwOnTimeout).then(() => toValue(r)).finally(() => stop == null ? void 0 : stop())
801
829
  );
802
830
  }
803
831
  return Promise.race(promises);
@@ -805,14 +833,14 @@ function createUntil(r, isNot = false) {
805
833
  function toBe(value, options) {
806
834
  if (!isRef(value))
807
835
  return toMatch((v) => v === value, options);
808
- const { flush = "sync", deep = false, timeout, throwOnTimeout } = options ?? {};
836
+ const { flush = "sync", deep = false, timeout, throwOnTimeout } = options != null ? options : {};
809
837
  let stop = null;
810
838
  const watcher = new Promise((resolve) => {
811
839
  stop = watch(
812
840
  [r, value],
813
841
  ([v1, v2]) => {
814
842
  if (isNot !== (v1 === v2)) {
815
- stop?.();
843
+ stop == null ? void 0 : stop();
816
844
  resolve(v1);
817
845
  }
818
846
  },
@@ -827,7 +855,7 @@ function createUntil(r, isNot = false) {
827
855
  if (timeout != null) {
828
856
  promises.push(
829
857
  promiseTimeout(timeout, throwOnTimeout).then(() => toValue(r)).finally(() => {
830
- stop?.();
858
+ stop == null ? void 0 : stop();
831
859
  return toValue(r);
832
860
  })
833
861
  );
@@ -898,9 +926,10 @@ function defaultComparator(value, othVal) {
898
926
  return value === othVal;
899
927
  }
900
928
  function useArrayDifference(...args) {
929
+ var _a;
901
930
  const list = args[0];
902
931
  const values = args[1];
903
- let compareFn = args[2] ?? defaultComparator;
932
+ let compareFn = (_a = args[2]) != null ? _a : defaultComparator;
904
933
  if (typeof compareFn === "string") {
905
934
  const key = compareFn;
906
935
  compareFn = (value, othVal) => value[key] === othVal[key];
@@ -917,11 +946,9 @@ function useArrayFilter(list, fn) {
917
946
  }
918
947
 
919
948
  function useArrayFind(list, fn) {
920
- return computed(
921
- () => toValue(
922
- toValue(list).find((element, index, array) => fn(toValue(element), index, array))
923
- )
924
- );
949
+ return computed(() => toValue(
950
+ toValue(list).find((element, index, array) => fn(toValue(element), index, array))
951
+ ));
925
952
  }
926
953
 
927
954
  function useArrayFindIndex(list, fn) {
@@ -937,35 +964,35 @@ function findLast(arr, cb) {
937
964
  return void 0;
938
965
  }
939
966
  function useArrayFindLast(list, fn) {
940
- return computed(
941
- () => toValue(
942
- !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))
943
- )
944
- );
967
+ return computed(() => toValue(
968
+ !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))
969
+ ));
945
970
  }
946
971
 
947
972
  function isArrayIncludesOptions(obj) {
948
973
  return isObject(obj) && containsProp(obj, "formIndex", "comparator");
949
974
  }
950
975
  function useArrayIncludes(...args) {
976
+ var _a;
951
977
  const list = args[0];
952
978
  const value = args[1];
953
979
  let comparator = args[2];
954
980
  let formIndex = 0;
955
981
  if (isArrayIncludesOptions(comparator)) {
956
- formIndex = comparator.fromIndex ?? 0;
982
+ formIndex = (_a = comparator.fromIndex) != null ? _a : 0;
957
983
  comparator = comparator.comparator;
958
984
  }
959
985
  if (typeof comparator === "string") {
960
986
  const key = comparator;
961
987
  comparator = (element, value2) => element[key] === toValue(value2);
962
988
  }
963
- comparator = comparator ?? ((element, value2) => element === toValue(value2));
964
- return computed(
965
- () => toValue(list).slice(formIndex).some(
966
- (element, index, array) => comparator(toValue(element), toValue(value), index, toValue(array))
967
- )
968
- );
989
+ comparator = comparator != null ? comparator : (element, value2) => element === toValue(value2);
990
+ return computed(() => toValue(list).slice(formIndex).some((element, index, array) => comparator(
991
+ toValue(element),
992
+ toValue(value),
993
+ index,
994
+ toValue(array)
995
+ )));
969
996
  }
970
997
 
971
998
  function useArrayJoin(list, separator) {
@@ -1032,6 +1059,7 @@ function defaultMeridiem(hours, minutes, isLowercase, hasPeriod) {
1032
1059
  return isLowercase ? m.toLowerCase() : m;
1033
1060
  }
1034
1061
  function formatDate(date, formatStr, options = {}) {
1062
+ var _a;
1035
1063
  const years = date.getFullYear();
1036
1064
  const month = date.getMonth();
1037
1065
  const days = date.getDate();
@@ -1040,7 +1068,7 @@ function formatDate(date, formatStr, options = {}) {
1040
1068
  const seconds = date.getSeconds();
1041
1069
  const milliseconds = date.getMilliseconds();
1042
1070
  const day = date.getDay();
1043
- const meridiem = options.customMeridiem ?? defaultMeridiem;
1071
+ const meridiem = (_a = options.customMeridiem) != null ? _a : defaultMeridiem;
1044
1072
  const matches = {
1045
1073
  YY: () => String(years).slice(-2),
1046
1074
  YYYY: () => years,
@@ -1068,7 +1096,10 @@ function formatDate(date, formatStr, options = {}) {
1068
1096
  a: () => meridiem(hours, minutes, true),
1069
1097
  aa: () => meridiem(hours, minutes, true, true)
1070
1098
  };
1071
- return formatStr.replace(REGEX_FORMAT, (match, $1) => $1 ?? matches[match]?.() ?? match);
1099
+ return formatStr.replace(REGEX_FORMAT, (match, $1) => {
1100
+ var _a2, _b;
1101
+ return (_b = $1 != null ? $1 : (_a2 = matches[match]) == null ? void 0 : _a2.call(matches)) != null ? _b : match;
1102
+ });
1072
1103
  }
1073
1104
  function normalizeDate(date) {
1074
1105
  if (date === null)
@@ -1166,7 +1197,8 @@ function useInterval(interval = 1e3, options = {}) {
1166
1197
  }
1167
1198
 
1168
1199
  function useLastChanged(source, options = {}) {
1169
- const ms = ref(options.initialValue ?? null);
1200
+ var _a;
1201
+ const ms = ref((_a = options.initialValue) != null ? _a : null);
1170
1202
  watch(
1171
1203
  source,
1172
1204
  () => ms.value = timestamp(),
@@ -1219,7 +1251,7 @@ function useTimeout(interval = 1e3, options = {}) {
1219
1251
  callback
1220
1252
  } = options;
1221
1253
  const controls = useTimeoutFn(
1222
- callback ?? noop,
1254
+ callback != null ? callback : noop,
1223
1255
  interval,
1224
1256
  options
1225
1257
  );
@@ -1278,9 +1310,7 @@ function useToggle(initialValue = false, options = {}) {
1278
1310
  }
1279
1311
 
1280
1312
  function watchArray(source, cb, options) {
1281
- let oldList = options?.immediate ? [] : [
1282
- ...source instanceof Function ? source() : Array.isArray(source) ? source : toValue(source)
1283
- ];
1313
+ let oldList = (options == null ? void 0 : options.immediate) ? [] : [...source instanceof Function ? source() : Array.isArray(source) ? source : toValue(source)];
1284
1314
  return watch(source, (newList, _, onCleanup) => {
1285
1315
  const oldListRemains = Array.from({ length: oldList.length });
1286
1316
  const added = [];
@@ -1506,4 +1536,4 @@ function whenever(source, cb, options) {
1506
1536
  );
1507
1537
  }
1508
1538
 
1509
- export { assert, refAutoReset as autoResetRef, bypassFilter, camelize, clamp, computedEager, computedWithControl, containsProp, computedWithControl as controlledComputed, controlledRef, createEventHook, createFilterWrapper, createGlobalState, createInjectionState, reactify as createReactiveFn, createSharedComposable, createSingletonPromise, debounceFilter, refDebounced as debouncedRef, watchDebounced as debouncedWatch, directiveHooks, computedEager as eagerComputed, extendRef, formatDate, get, hasOwn, hyphenate, identity, watchIgnorable as ignorableWatch, increaseWithUnit, invoke, isClient, isDef, isDefined, isIOS, isObject, makeDestructurable, noop, normalizeDate, notNullish, now, objectEntries, objectOmit, objectPick, pausableFilter, watchPausable as pausableWatch, promiseTimeout, rand, reactify, reactifyObject, reactiveComputed, reactiveOmit, reactivePick, refAutoReset, refDebounced, refDefault, refThrottled, refWithControl, resolveRef, resolveUnref, set, syncRef, syncRefs, throttleFilter, refThrottled as throttledRef, watchThrottled as throttledWatch, timestamp, toReactive, toRef, toRefs, toValue, tryOnBeforeMount, tryOnBeforeUnmount, tryOnMounted, tryOnScopeDispose, tryOnUnmounted, until, useArrayDifference, useArrayEvery, useArrayFilter, useArrayFind, useArrayFindIndex, useArrayFindLast, useArrayIncludes, useArrayJoin, useArrayMap, useArrayReduce, useArraySome, useArrayUnique, useCounter, useDateFormat, refDebounced as useDebounce, useDebounceFn, useInterval, useIntervalFn, useLastChanged, refThrottled as useThrottle, useThrottleFn, useTimeout, useTimeoutFn, useToNumber, useToString, useToggle, watchArray, watchAtMost, watchDebounced, watchDeep, watchIgnorable, watchImmediate, watchOnce, watchPausable, watchThrottled, watchTriggerable, watchWithFilter, whenever };
1539
+ export { assert, refAutoReset as autoResetRef, bypassFilter, camelize, clamp, computedEager, computedWithControl, containsProp, computedWithControl as controlledComputed, controlledRef, createEventHook, createFilterWrapper, createGlobalState, createInjectionState, reactify as createReactiveFn, createSharedComposable, createSingletonPromise, debounceFilter, refDebounced as debouncedRef, watchDebounced as debouncedWatch, directiveHooks, computedEager as eagerComputed, extendRef, formatDate, get, hasOwn, hyphenate, identity, watchIgnorable as ignorableWatch, increaseWithUnit, injectLocal, invoke, isClient, isDef, isDefined, isIOS, isObject, makeDestructurable, noop, normalizeDate, notNullish, now, objectEntries, objectOmit, objectPick, pausableFilter, watchPausable as pausableWatch, promiseTimeout, provideLocal, rand, reactify, reactifyObject, reactiveComputed, reactiveOmit, reactivePick, refAutoReset, refDebounced, refDefault, refThrottled, refWithControl, resolveRef, resolveUnref, set, syncRef, syncRefs, throttleFilter, refThrottled as throttledRef, watchThrottled as throttledWatch, timestamp, toReactive, toRef, toRefs, toValue, tryOnBeforeMount, tryOnBeforeUnmount, tryOnMounted, tryOnScopeDispose, tryOnUnmounted, until, useArrayDifference, useArrayEvery, useArrayFilter, useArrayFind, useArrayFindIndex, useArrayFindLast, useArrayIncludes, useArrayJoin, useArrayMap, useArrayReduce, useArraySome, useArrayUnique, useCounter, useDateFormat, refDebounced as useDebounce, useDebounceFn, useInterval, useIntervalFn, useLastChanged, refThrottled as useThrottle, useThrottleFn, useTimeout, useTimeoutFn, useToNumber, useToString, useToggle, watchArray, watchAtMost, watchDebounced, watchDeep, watchIgnorable, watchImmediate, watchOnce, watchPausable, watchThrottled, watchTriggerable, watchWithFilter, whenever };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@vueuse/shared",
3
- "version": "10.4.0",
3
+ "version": "10.5.0",
4
4
  "author": "Anthony Fu <https://github.com/antfu>",
5
5
  "license": "MIT",
6
6
  "funding": "https://github.com/sponsors/antfu",
@@ -32,6 +32,6 @@
32
32
  "jsdelivr": "./index.iife.min.js",
33
33
  "types": "./index.d.cts",
34
34
  "dependencies": {
35
- "vue-demi": ">=0.14.5"
35
+ "vue-demi": ">=0.14.6"
36
36
  }
37
37
  }