Skip to content

Commit

Permalink
feat(utils): hasReactive
Browse files Browse the repository at this point in the history
  • Loading branch information
wangcch committed Mar 24, 2023
1 parent 2f32649 commit b50bfc6
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 3 deletions.
12 changes: 11 additions & 1 deletion src/utils.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import type { ComputedRef, Ref } from "vue";
import { readonly, ref, unref } from "vue";
import { isReactive, readonly, ref, unref } from "vue";

type Reducer<S, A> = (prevState: S, action: A) => S;
type ReducerState<R extends Reducer<any, any>> = R extends Reducer<infer S, any>
Expand Down Expand Up @@ -45,3 +45,13 @@ export function unrefs<T extends any[]>(
}
return [] as unknown as UnRefArrayItem<T>;
}

/**
* check whether `reactive` value exists
*/
export function hasReactive<T>(args: T) {
if (args && Array.isArray(args)) {
return args.some(isReactive);
}
return false;
}
24 changes: 22 additions & 2 deletions tests/utils.test.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { describe, test, expect } from "vitest";
import { isRef, ref, unref } from "vue";
import { isRef, ref, reactive, unref } from "vue";

import { unrefs, useReducer } from "../src/utils";
import { unrefs, useReducer, hasReactive } from "../src/utils";

describe("unrefs", () => {
test("should be defined", () => {
Expand Down Expand Up @@ -71,3 +71,23 @@ describe("useReducer", () => {
expect(state.value.num).toBe(2);
});
});

describe("hasReactive", () => {
test("should be defined", () => {
expect(hasReactive).toBeDefined();
});

test("check", () => {
expect(hasReactive(null)).toBeFalsy();
expect(hasReactive(undefined)).toBeFalsy();
expect(hasReactive({})).toBeFalsy();
expect(hasReactive({ 0: 1, 1: 2 })).toBeFalsy();
expect(hasReactive([])).toBeFalsy();
expect(hasReactive([1, 2, 3])).toBeFalsy();
expect(hasReactive([ref(1), 2, 3])).toBeFalsy();
expect(hasReactive([ref(1), ref(2), 3])).toBeFalsy();
expect(hasReactive([ref(1), ref(2), { a: 1 }])).toBeFalsy();
expect(hasReactive([reactive({ a: 1 })])).toBeTruthy();
expect(hasReactive([1, 2, reactive({ a: 1 })])).toBeTruthy();
});
});

0 comments on commit b50bfc6

Please sign in to comment.