@vueuse/shared 8.9.3 → 9.0.0-beta.1

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,4 +1,5 @@
1
- import { shallowRef, watchEffect, readonly, ref, unref, isVue3, watch, customRef, effectScope, provide, inject, getCurrentScope, onScopeDispose, isRef, computed, reactive, toRefs as toRefs$1, toRef, isVue2, set as set$1, getCurrentInstance, onBeforeMount, nextTick, onBeforeUnmount, onMounted, onUnmounted, isReactive } from 'vue-demi';
1
+ import { shallowRef, watchEffect, readonly, unref, ref, isVue3, watch, customRef, effectScope, provide, inject, getCurrentScope, onScopeDispose, isRef, computed, reactive, toRefs as toRefs$1, toRef, isVue2, set as set$1, getCurrentInstance, onBeforeMount, nextTick, onBeforeUnmount, onMounted, onUnmounted, isReactive } from 'vue-demi';
2
+ import { resolveUnref as resolveUnref$1 } from '@vueuse/shared';
2
3
 
3
4
  var __defProp$9 = Object.defineProperty;
4
5
  var __defProps$6 = Object.defineProperties;
@@ -56,6 +57,10 @@ const rand = (min, max) => {
56
57
  };
57
58
  const isIOS = isClient && ((_a = window == null ? void 0 : window.navigator) == null ? void 0 : _a.userAgent) && /iP(ad|hone|od)/.test(window.navigator.userAgent);
58
59
 
60
+ function resolveUnref(r) {
61
+ return typeof r === "function" ? r() : unref(r);
62
+ }
63
+
59
64
  function createFilterWrapper(filter, fn) {
60
65
  function wrapper(...args) {
61
66
  filter(() => fn.apply(this, args), { fn, thisArg: this, args });
@@ -69,8 +74,8 @@ function debounceFilter(ms, options = {}) {
69
74
  let timer;
70
75
  let maxTimer;
71
76
  const filter = (invoke) => {
72
- const duration = unref(ms);
73
- const maxDuration = unref(options.maxWait);
77
+ const duration = resolveUnref(ms);
78
+ const maxDuration = resolveUnref(options.maxWait);
74
79
  if (timer)
75
80
  clearTimeout(timer);
76
81
  if (duration <= 0 || maxDuration !== void 0 && maxDuration <= 0) {
@@ -108,7 +113,7 @@ function throttleFilter(ms, trailing = true, leading = true) {
108
113
  }
109
114
  };
110
115
  const filter = (invoke) => {
111
- const duration = unref(ms);
116
+ const duration = resolveUnref(ms);
112
117
  const elapsed = Date.now() - lastExec;
113
118
  clear();
114
119
  if (duration <= 0) {
@@ -353,18 +358,6 @@ function isDefined(v) {
353
358
  return unref(v) != null;
354
359
  }
355
360
 
356
- function logicAnd(...args) {
357
- return computed(() => args.every((i) => unref(i)));
358
- }
359
-
360
- function logicNot(v) {
361
- return computed(() => !unref(v));
362
- }
363
-
364
- function logicOr(...args) {
365
- return computed(() => args.some((i) => unref(i)));
366
- }
367
-
368
361
  var __defProp$8 = Object.defineProperty;
369
362
  var __getOwnPropSymbols$a = Object.getOwnPropertySymbols;
370
363
  var __hasOwnProp$a = Object.prototype.hasOwnProperty;
@@ -402,17 +395,20 @@ function makeDestructurable(obj, arr) {
402
395
  }
403
396
  }
404
397
 
405
- function reactify(fn) {
398
+ function reactify(fn, options) {
399
+ const unrefFn = (options == null ? void 0 : options.computedGetter) === false ? unref : resolveUnref;
406
400
  return function(...args) {
407
- return computed(() => fn.apply(this, args.map((i) => unref(i))));
401
+ return computed(() => fn.apply(this, args.map((i) => unrefFn(i))));
408
402
  };
409
403
  }
410
404
 
411
405
  function reactifyObject(obj, optionsOrKeys = {}) {
412
406
  let keys = [];
407
+ let options;
413
408
  if (Array.isArray(optionsOrKeys)) {
414
409
  keys = optionsOrKeys;
415
410
  } else {
411
+ options = optionsOrKeys;
416
412
  const { includeOwnProperties = true } = optionsOrKeys;
417
413
  keys.push(...Object.keys(obj));
418
414
  if (includeOwnProperties)
@@ -422,7 +418,7 @@ function reactifyObject(obj, optionsOrKeys = {}) {
422
418
  const value = obj[key];
423
419
  return [
424
420
  key,
425
- typeof value === "function" ? reactify(value.bind(obj)) : value
421
+ typeof value === "function" ? reactify(value.bind(obj), options) : value
426
422
  ];
427
423
  }));
428
424
  }
@@ -481,7 +477,7 @@ function refAutoReset(defaultValue, afterMs = 1e4) {
481
477
  const resetAfter = () => setTimeout(() => {
482
478
  value = defaultValue;
483
479
  trigger();
484
- }, unref(afterMs));
480
+ }, resolveUnref$1(afterMs));
485
481
  tryOnScopeDispose(() => {
486
482
  clearTimeout(timer);
487
483
  });
@@ -527,7 +523,7 @@ function refDefault(source, defaultValue) {
527
523
  });
528
524
  }
529
525
 
530
- function useThrottleFn(fn, ms = 200, trailing = true, leading = true) {
526
+ function useThrottleFn(fn, ms = 200, trailing = false, leading = true) {
531
527
  return createFilterWrapper(throttleFilter(ms, trailing, leading), fn);
532
528
  }
533
529
 
@@ -594,10 +590,6 @@ function resolveRef(r) {
594
590
  return typeof r === "function" ? computed(r) : ref(r);
595
591
  }
596
592
 
597
- function resolveUnref(r) {
598
- return typeof r === "function" ? r() : unref(r);
599
- }
600
-
601
593
  function set(...args) {
602
594
  if (args.length === 2) {
603
595
  const [ref, value] = args;
@@ -734,7 +726,7 @@ function until(r) {
734
726
  });
735
727
  const promises = [watcher];
736
728
  if (timeout != null) {
737
- promises.push(promiseTimeout(timeout, throwOnTimeout).then(() => unref(r)).finally(() => stop == null ? void 0 : stop()));
729
+ promises.push(promiseTimeout(timeout, throwOnTimeout).then(() => resolveUnref(r)).finally(() => stop == null ? void 0 : stop()));
738
730
  }
739
731
  return Promise.race(promises);
740
732
  }
@@ -757,9 +749,9 @@ function until(r) {
757
749
  });
758
750
  const promises = [watcher];
759
751
  if (timeout != null) {
760
- promises.push(promiseTimeout(timeout, throwOnTimeout).then(() => unref(r)).finally(() => {
752
+ promises.push(promiseTimeout(timeout, throwOnTimeout).then(() => resolveUnref(r)).finally(() => {
761
753
  stop == null ? void 0 : stop();
762
- return unref(r);
754
+ return resolveUnref(r);
763
755
  }));
764
756
  }
765
757
  return Promise.race(promises);
@@ -779,7 +771,7 @@ function until(r) {
779
771
  function toContains(value, options) {
780
772
  return toMatch((v) => {
781
773
  const array = Array.from(v);
782
- return array.includes(value) || array.includes(unref(value));
774
+ return array.includes(value) || array.includes(resolveUnref(value));
783
775
  }, options);
784
776
  }
785
777
  function changed(options) {
@@ -792,7 +784,7 @@ function until(r) {
792
784
  return count >= n;
793
785
  }, options);
794
786
  }
795
- if (Array.isArray(unref(r))) {
787
+ if (Array.isArray(resolveUnref(r))) {
796
788
  const instance = {
797
789
  toMatch,
798
790
  toContains,
@@ -823,6 +815,38 @@ function until(r) {
823
815
  }
824
816
  }
825
817
 
818
+ function useArrayEvery(list, fn) {
819
+ return computed(() => resolveUnref$1(list).every((element, index, array) => fn(resolveUnref$1(element), index, array)));
820
+ }
821
+
822
+ function useArrayFilter(list, fn) {
823
+ return computed(() => resolveUnref$1(list).map((i) => resolveUnref$1(i)).filter(fn));
824
+ }
825
+
826
+ function useArrayFind(list, fn) {
827
+ return computed(() => resolveUnref$1(resolveUnref$1(list).find((element, index, array) => fn(resolveUnref$1(element), index, array))));
828
+ }
829
+
830
+ function useArrayJoin(list, separator) {
831
+ return computed(() => resolveUnref$1(list).map((i) => resolveUnref$1(i)).join(resolveUnref$1(separator)));
832
+ }
833
+
834
+ function useArrayMap(list, fn) {
835
+ return computed(() => resolveUnref$1(list).map((i) => resolveUnref$1(i)).map(fn));
836
+ }
837
+
838
+ function useArrayReduce(list, reducer, ...args) {
839
+ const reduceCallback = (sum, value, index) => reducer(resolveUnref$1(sum), resolveUnref$1(value), index);
840
+ return computed(() => {
841
+ const resolved = resolveUnref$1(list);
842
+ return args.length ? resolved.reduce(reduceCallback, resolveUnref$1(args[0])) : resolved.reduce(reduceCallback);
843
+ });
844
+ }
845
+
846
+ function useArraySome(list, fn) {
847
+ return computed(() => resolveUnref$1(list).some((element, index, array) => fn(resolveUnref$1(element), index, array)));
848
+ }
849
+
826
850
  function useCounter(initialValue = 0, options = {}) {
827
851
  const count = ref(initialValue);
828
852
  const {
@@ -889,7 +913,7 @@ const normalizeDate = (date) => {
889
913
  return new Date(date);
890
914
  };
891
915
  function useDateFormat(date, formatStr = "HH:mm:ss") {
892
- return computed(() => formatDate(normalizeDate(unref(date)), unref(formatStr)));
916
+ return computed(() => formatDate(normalizeDate(resolveUnref$1(date)), resolveUnref$1(formatStr)));
893
917
  }
894
918
 
895
919
  function useIntervalFn(cb, interval = 1e3, options = {}) {
@@ -916,7 +940,7 @@ function useIntervalFn(cb, interval = 1e3, options = {}) {
916
940
  if (immediateCallback)
917
941
  cb();
918
942
  clean();
919
- timer = setInterval(cb, unref(interval));
943
+ timer = setInterval(cb, resolveUnref(interval));
920
944
  }
921
945
  if (immediate && isClient)
922
946
  resume();
@@ -997,7 +1021,7 @@ function useTimeoutFn(cb, interval, options = {}) {
997
1021
  isPending.value = false;
998
1022
  timer = null;
999
1023
  cb(...args);
1000
- }, unref(interval));
1024
+ }, resolveUnref$1(interval));
1001
1025
  }
1002
1026
  if (immediate) {
1003
1027
  isPending.value = true;
@@ -1049,20 +1073,21 @@ function useToggle(initialValue = false, options = {}) {
1049
1073
  falsyValue = false
1050
1074
  } = options;
1051
1075
  const valueIsRef = isRef(initialValue);
1052
- const innerValue = ref(initialValue);
1076
+ const _value = ref(initialValue);
1053
1077
  function toggle(value) {
1054
1078
  if (arguments.length) {
1055
- innerValue.value = value;
1056
- return innerValue.value;
1079
+ _value.value = value;
1080
+ return _value.value;
1057
1081
  } else {
1058
- innerValue.value = innerValue.value === unref(truthyValue) ? unref(falsyValue) : unref(truthyValue);
1059
- return innerValue.value;
1082
+ const truthy = resolveUnref(truthyValue);
1083
+ _value.value = _value.value === truthy ? resolveUnref(falsyValue) : truthy;
1084
+ return _value.value;
1060
1085
  }
1061
1086
  }
1062
1087
  if (valueIsRef)
1063
1088
  return toggle;
1064
1089
  else
1065
- return [innerValue, toggle];
1090
+ return [_value, toggle];
1066
1091
  }
1067
1092
 
1068
1093
  function watchArray(source, cb, options) {
@@ -1138,7 +1163,7 @@ function watchAtMost(source, cb, options) {
1138
1163
  const current = ref(0);
1139
1164
  const stop = watchWithFilter(source, (...args) => {
1140
1165
  current.value += 1;
1141
- if (current.value >= unref(count))
1166
+ if (current.value >= resolveUnref(count))
1142
1167
  nextTick(() => stop());
1143
1168
  cb(...args);
1144
1169
  }, watchOptions);
@@ -1439,4 +1464,4 @@ function whenever(source, cb, options) {
1439
1464
  }, options);
1440
1465
  }
1441
1466
 
1442
- export { __onlyVue3, logicAnd as and, assert, refAutoReset as autoResetRef, bypassFilter, 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, identity, watchIgnorable as ignorableWatch, increaseWithUnit, invoke, isBoolean, isClient, isDef, isDefined, isFunction, isIOS, isNumber, isObject, isString, isWindow, logicAnd, logicNot, logicOr, makeDestructurable, noop, normalizeDate, logicNot as not, now, objectPick, logicOr as or, 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, toRefs, tryOnBeforeMount, tryOnBeforeUnmount, tryOnMounted, tryOnScopeDispose, tryOnUnmounted, until, useCounter, useDateFormat, refDebounced as useDebounce, useDebounceFn, useInterval, useIntervalFn, useLastChanged, refThrottled as useThrottle, useThrottleFn, useTimeout, useTimeoutFn, useToggle, watchArray, watchAtMost, watchDebounced, watchIgnorable, watchOnce, watchPausable, watchThrottled, watchTriggerable, watchWithFilter, whenever };
1467
+ export { __onlyVue3, assert, refAutoReset as autoResetRef, bypassFilter, 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, identity, watchIgnorable as ignorableWatch, increaseWithUnit, invoke, isBoolean, isClient, isDef, isDefined, isFunction, isIOS, isNumber, isObject, isString, isWindow, makeDestructurable, noop, normalizeDate, now, 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, toRefs, tryOnBeforeMount, tryOnBeforeUnmount, tryOnMounted, tryOnScopeDispose, tryOnUnmounted, until, useArrayEvery, useArrayFilter, useArrayFind, useArrayJoin, useArrayMap, useArrayReduce, useArraySome, useCounter, useDateFormat, refDebounced as useDebounce, useDebounceFn, useInterval, useIntervalFn, useLastChanged, refThrottled as useThrottle, useThrottleFn, useTimeout, useTimeoutFn, useToggle, watchArray, watchAtMost, watchDebounced, watchIgnorable, watchOnce, watchPausable, watchThrottled, watchTriggerable, watchWithFilter, whenever };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@vueuse/shared",
3
- "version": "8.9.3",
3
+ "version": "9.0.0-beta.1",
4
4
  "author": "Anthony Fu <https://github.com/antfu>",
5
5
  "license": "MIT",
6
6
  "funding": "https://github.com/sponsors/antfu",
@@ -32,18 +32,6 @@
32
32
  "unpkg": "./index.iife.min.js",
33
33
  "jsdelivr": "./index.iife.min.js",
34
34
  "types": "./index.d.ts",
35
- "peerDependencies": {
36
- "@vue/composition-api": "^1.1.0",
37
- "vue": "^2.6.0 || ^3.2.0"
38
- },
39
- "peerDependenciesMeta": {
40
- "vue": {
41
- "optional": true
42
- },
43
- "@vue/composition-api": {
44
- "optional": true
45
- }
46
- },
47
35
  "dependencies": {
48
36
  "vue-demi": "*"
49
37
  }