limu
Version:
A fast js lib of immutable data, based on shallow copy on read and mark modified on write mechanism
36 lines (35 loc) • 1.15 kB
JavaScript
/*---------------------------------------------------------------------------------------------
* Licensed under the MIT License.
*
* @Author: fantasticsoul
*--------------------------------------------------------------------------------------------*/
import { IS_RAW } from '../support/consts';
import { isPrimitive } from '../support/util';
import { deepCopy } from './copy';
import { getDraftMeta } from './meta';
export function original(mayDraftProxy) {
if (!mayDraftProxy) {
return mayDraftProxy;
}
const meta = getDraftMeta(mayDraftProxy);
// use ternary conditional operator instead of meta?.self
// avoid generating redundant compiled code
const self = meta ? meta.self : mayDraftProxy;
return self;
}
export function current(mayDraftProxy) {
if (!mayDraftProxy) {
return mayDraftProxy;
}
const meta = getDraftMeta(mayDraftProxy);
if (!meta) {
return mayDraftProxy;
}
return deepCopy(meta.copy || meta.self);
}
export function markRaw(rawVal) {
if (!rawVal || isPrimitive(rawVal))
return rawVal;
rawVal[IS_RAW] = true;
return rawVal;
}