• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

unadlib / mutative / #50

pending completion
#50

push

GitHub
<a href="https://github.com/unadlib/mutative/commit/<a class=hub.com/unadlib/mutative/commit/df87ee97a928238a2d33a19cd6ae8c76ffc6b08b">df87ee97a<a href="https://github.com/unadlib/mutative/commit/df87ee97a928238a2d33a19cd6ae8c76ffc6b08b">">Merge </a><a class="double-link" href="https://github.com/unadlib/mutative/commit/<a class="double-link" href="https://github.com/unadlib/mutative/commit/451be92b8fd8a0c81ed092133199f29600365da9">451be92b8</a>">451be92b8</a><a href="https://github.com/unadlib/mutative/commit/df87ee97a928238a2d33a19cd6ae8c76ffc6b08b"> into e5ffa1bfc">e5ffa1bfc</a>

533 of 536 branches covered (99.44%)

Branch coverage included in aggregate %.

36 of 36 new or added lines in 3 files covered. (100.0%)

797 of 798 relevant lines covered (99.87%)

843.58 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

99.08
/src/utils/draft.ts
1
import { DraftType, Mark, ProxyDraft } from '../interface';
2
import { dataTypes, PROXY_DRAFT } from '../constant';
32✔
3

4
export function latest<T = any>(proxyDraft: ProxyDraft): T {
32✔
5
  return proxyDraft.copy ?? proxyDraft.original;
11,120✔
6
}
7

8
/**
9
 * Check if the value is a draft
10
 */
11
export function isDraft(target: any) {
32✔
12
  return !!getProxyDraft(target);
7,276✔
13
}
14

15
export function getProxyDraft<T extends any>(value: T): ProxyDraft | null {
32✔
16
  if (typeof value !== 'object') return null;
28,487✔
17
  return (value as { [PROXY_DRAFT]: any })?.[PROXY_DRAFT];
19,492✔
18
}
19

20
export function getValue<T extends object>(value: T): T {
32✔
21
  const proxyDraft = getProxyDraft(value);
2,263✔
22
  return proxyDraft ? proxyDraft.copy ?? proxyDraft.original : value;
2,263✔
23
}
24

25
/**
26
 * Check if a value is draftable
27
 */
28
export function isDraftable(value: any, options?: { mark?: Mark<any, any> }) {
32✔
29
  if (!value || typeof value !== 'object') return false;
12,388✔
30
  let markResult: any;
31
  return (
7,632✔
32
    Object.getPrototypeOf(value) === Object.prototype ||
12,368✔
33
    Array.isArray(value) ||
34
    value instanceof Map ||
35
    value instanceof Set ||
36
    (!!options?.mark &&
708✔
37
      ((markResult = options.mark(value, dataTypes)) === dataTypes.immutable ||
38
        typeof markResult === 'function'))
39
  );
40
}
41

42
export function getPath(
32✔
43
  target: ProxyDraft,
44
  path: any[] = []
482✔
45
): (string | number | object)[] {
46
  if (Object.hasOwnProperty.call(target, 'key'))
804✔
47
    path.push(
322✔
48
      target.parent!.type === DraftType.Set
49
        ? Array.from(target.parent!.setMap!.keys()).indexOf(target.key)
322✔
50
        : target.key
51
    );
52
  if (target.parent) {
804✔
53
    return getPath(target.parent, path);
322✔
54
  }
55
  return path.reverse();
482✔
56
}
57

58
export function getType(target: any) {
32✔
59
  if (Array.isArray(target)) return DraftType.Array;
12,027✔
60
  if (target instanceof Map) return DraftType.Map;
10,222✔
61
  if (target instanceof Set) return DraftType.Set;
8,885✔
62
  return DraftType.Object;
8,428✔
63
}
64

65
export function get(target: any, key: PropertyKey) {
32✔
66
  return getType(target) === DraftType.Map ? target.get(key) : target[key];
4,565✔
67
}
68

69
export function set(target: any, key: PropertyKey, value: any) {
32✔
70
  if (getType(target) === DraftType.Map) {
1,401✔
71
    target.set(key, value);
147✔
72
  } else {
73
    target[key] = value;
1,254✔
74
  }
75
}
76

77
export function peek(target: any, key: PropertyKey) {
32✔
78
  const state = getProxyDraft(target);
4,405✔
79
  const source = state ? latest(state) : target;
4,405✔
80
  return source[key];
4,405✔
81
}
82

83
export function isEqual(x: any, y: any) {
32✔
84
  if (x === y) {
4,803✔
85
    return x !== 0 || 1 / x === 1 / y;
897✔
86
  } else {
87
    return x !== x && y !== y;
3,906✔
88
  }
89
}
90

91
export function revokeProxy(proxyDraft: ProxyDraft | null) {
32✔
92
  if (!proxyDraft) return;
1,780✔
93
  while (proxyDraft.finalities.revoke.length > 0) {
1,777✔
94
    const revoke = proxyDraft.finalities.revoke.pop()!;
3,315✔
95
    revoke();
3,315✔
96
  }
97
}
98

99
export function escapePath(path: string[], pathAsArray: boolean) {
32✔
100
  return pathAsArray
911✔
101
    ? path
911✔
102
    : ['']
103
        .concat(path)
104
        .map((_item) => {
105
          const item = `${_item}`;
98✔
106
          if (item.indexOf('/') === -1 && item.indexOf('~') === -1) return item;
98✔
107
          return item.replace(/~/g, '~0').replace(/\//g, '~1');
×
108
        })
109
        .join('/');
110
}
111

112
export function unescapePath(path: string | (string | number)[]) {
32✔
113
  if (Array.isArray(path)) return path;
636✔
114
  return path
41✔
115
    .split('/')
116
    .map((_item) => _item.replace(/~1/g, '/').replace(/~0/g, '~'))
108✔
117
    .slice(1);
118
}
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2026 Coveralls, Inc