diff --git a/src/readonly/index.ts b/src/readonly/index.ts index 3bf45372..e5781fa8 100644 --- a/src/readonly/index.ts +++ b/src/readonly/index.ts @@ -1,9 +1,18 @@ -import { Store, Event, is, combine } from 'effector'; +import { Store, Event, is, createStore } from 'effector'; +export function readonly(source: T): Store; export function readonly(source: Store): Store; export function readonly(source: Event): Event; export function readonly(source: Store | Event) { + if (!is.unit(source)) { + if (typeof source === 'function' || source === undefined) { + return source; + } + + return createStore(source).map((value) => value, { skipVoid: false }); + } + if (!is.targetable(source)) { return source; } diff --git a/src/readonly/readonly.test.ts b/src/readonly/readonly.test.ts index 5e20cee4..e14eeb0a 100644 --- a/src/readonly/readonly.test.ts +++ b/src/readonly/readonly.test.ts @@ -30,3 +30,32 @@ it('should return event as-is if it is already derived', () => { expect(result).toBe(mapped); }); + +it('should convert non unit value to store', () => { + expect(is.store(readonly(1))).toBe(true); + expect(is.store(readonly('12'))).toBe(true); + expect(is.store(readonly(''))).toBe(true); + expect(is.store(readonly(true))).toBe(true); + expect(is.store(readonly(false))).toBe(true); + expect(is.store(readonly([]))).toBe(true); + expect(is.store(readonly({}))).toBe(true); + expect(is.store(readonly(null))).toBe(true); + expect(is.store(readonly(null))).toBe(true); + + expect(is.targetable(readonly(1))).toBe(false); + expect(is.targetable(readonly('12'))).toBe(false); + expect(is.targetable(readonly(''))).toBe(false); + expect(is.targetable(readonly(true))).toBe(false); + expect(is.targetable(readonly(false))).toBe(false); + expect(is.targetable(readonly([]))).toBe(false); + expect(is.targetable(readonly({}))).toBe(false); + expect(is.targetable(readonly(null))).toBe(false); + expect(is.targetable(readonly(null))).toBe(false); +}); + +it('sould return value as-is if it is not unit and function or undefined', () => { + expect(is.store(readonly(() => {}))).toBe(false); + expect(is.store(readonly(undefined))).toBe(false); + expect(is.targetable(readonly(() => {}))).toBe(false); + expect(is.targetable(readonly(undefined))).toBe(false); +});