Skip to content

Commit b50bfc6

Browse files
committed
feat(utils): hasReactive
1 parent 2f32649 commit b50bfc6

File tree

2 files changed

+33
-3
lines changed

2 files changed

+33
-3
lines changed

src/utils.ts

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import type { ComputedRef, Ref } from "vue";
2-
import { readonly, ref, unref } from "vue";
2+
import { isReactive, readonly, ref, unref } from "vue";
33

44
type Reducer<S, A> = (prevState: S, action: A) => S;
55
type ReducerState<R extends Reducer<any, any>> = R extends Reducer<infer S, any>
@@ -45,3 +45,13 @@ export function unrefs<T extends any[]>(
4545
}
4646
return [] as unknown as UnRefArrayItem<T>;
4747
}
48+
49+
/**
50+
* check whether `reactive` value exists
51+
*/
52+
export function hasReactive<T>(args: T) {
53+
if (args && Array.isArray(args)) {
54+
return args.some(isReactive);
55+
}
56+
return false;
57+
}

tests/utils.test.ts

Lines changed: 22 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import { describe, test, expect } from "vitest";
2-
import { isRef, ref, unref } from "vue";
2+
import { isRef, ref, reactive, unref } from "vue";
33

4-
import { unrefs, useReducer } from "../src/utils";
4+
import { unrefs, useReducer, hasReactive } from "../src/utils";
55

66
describe("unrefs", () => {
77
test("should be defined", () => {
@@ -71,3 +71,23 @@ describe("useReducer", () => {
7171
expect(state.value.num).toBe(2);
7272
});
7373
});
74+
75+
describe("hasReactive", () => {
76+
test("should be defined", () => {
77+
expect(hasReactive).toBeDefined();
78+
});
79+
80+
test("check", () => {
81+
expect(hasReactive(null)).toBeFalsy();
82+
expect(hasReactive(undefined)).toBeFalsy();
83+
expect(hasReactive({})).toBeFalsy();
84+
expect(hasReactive({ 0: 1, 1: 2 })).toBeFalsy();
85+
expect(hasReactive([])).toBeFalsy();
86+
expect(hasReactive([1, 2, 3])).toBeFalsy();
87+
expect(hasReactive([ref(1), 2, 3])).toBeFalsy();
88+
expect(hasReactive([ref(1), ref(2), 3])).toBeFalsy();
89+
expect(hasReactive([ref(1), ref(2), { a: 1 }])).toBeFalsy();
90+
expect(hasReactive([reactive({ a: 1 })])).toBeTruthy();
91+
expect(hasReactive([1, 2, reactive({ a: 1 })])).toBeTruthy();
92+
});
93+
});

0 commit comments

Comments
 (0)