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

realm / realm-js / 7290294576

21 Dec 2023 03:46PM UTC coverage: 0.0% (-86.5%) from 86.51%
7290294576

Pull #6257

github

kneth
Calculate estimate for PBS. Deprecate old arguments
Pull Request #6257: Progress notification for Flexible Sync

0 of 1275 branches covered (0.0%)

Branch coverage included in aggregate %.

0 of 7 new or added lines in 1 file covered. (0.0%)

2258 existing lines in 60 files now uncovered.

0 of 2582 relevant lines covered (0.0%)

0.0 hits per line

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

0.0
/packages/realm/src/assert.ts
1
////////////////////////////////////////////////////////////////////////////
2
//
3
// Copyright 2022 Realm Inc.
4
//
5
// Licensed under the Apache License, Version 2.0 (the "License");
6
// you may not use this file except in compliance with the License.
7
// You may obtain a copy of the License at
8
//
9
// http://www.apache.org/licenses/LICENSE-2.0
10
//
11
// Unless required by applicable law or agreed to in writing, software
12
// distributed under the License is distributed on an "AS IS" BASIS,
13
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14
// See the License for the specific language governing permissions and
15
// limitations under the License.
16
//
17
////////////////////////////////////////////////////////////////////////////
18

19
import { AssertionError, BSON, DefaultObject, PrimaryKey, Realm, TypeAssertionError, binding } from "./internal";
20

21
/**
22
 * Expects the condition to be truthy
23
 * @throws an {@link Error} If the condition is not truthy. Throws either the {@link err} given as param if it's an {@link Error},
24
 * an {@link AssertionError} wrapping {@link err} if it's a string or undefined, or uses the result of invoking {@link err} if it's a function.
25
 * @param condition The condition that must be truthy to avoid throwing.
26
 * @param err Optional message or error to throw.
27
 * Or a function producing this, which is useful to avoid computing the error message in case it's not needed.
28
 */
29
export function assert(
30
  condition: unknown,
31
  err?: string | Error | (() => undefined | string | Error),
32
): asserts condition {
UNCOV
33
  if (!condition) {
×
34
    // Call any function to generate the error lazily
UNCOV
35
    err = typeof err === "function" ? err() : err;
×
UNCOV
36
    if (err instanceof Error) {
×
UNCOV
37
      throw err;
×
UNCOV
38
    } else if (typeof err === "string" || typeof err === "undefined") {
×
UNCOV
39
      throw new AssertionError(err);
×
40
    } else {
41
      throw new Error("Expected err to be an Err, string, undefined or a function returning either.");
×
42
    }
43
  }
44
}
45

46
/* eslint-disable-next-line @typescript-eslint/ban-types */
UNCOV
47
assert.instanceOf = <T extends Function>(
×
48
  value: unknown,
49
  constructor: T,
50
  target?: string,
51
): asserts value is T["prototype"] => {
UNCOV
52
  assert(
×
53
    value instanceof constructor,
UNCOV
54
    () => new TypeAssertionError(`an instance of ${constructor.name}`, value, target),
×
55
  );
56
};
57

UNCOV
58
assert.string = (value: unknown, target?: string): asserts value is string => {
×
UNCOV
59
  assert(typeof value === "string", () => new TypeAssertionError("a string", value, target));
×
60
};
61

UNCOV
62
assert.number = (value: unknown, target?: string): asserts value is number => {
×
UNCOV
63
  assert(typeof value === "number", () => new TypeAssertionError("a number", value, target));
×
64
};
65

UNCOV
66
assert.numericString = (value: unknown, target?: string) => {
×
UNCOV
67
  assert.string(value);
×
UNCOV
68
  assert(/^-?\d+$/.test(value), () => new TypeAssertionError("a numeric string", value, target));
×
69
};
70

UNCOV
71
assert.boolean = (value: unknown, target?: string): asserts value is boolean => {
×
UNCOV
72
  assert(typeof value === "boolean", () => new TypeAssertionError("a boolean", value, target));
×
73
};
74

75
/* eslint-disable-next-line @typescript-eslint/ban-types */
UNCOV
76
assert.function = (value: unknown, target?: string): asserts value is (...args: unknown[]) => unknown => {
×
UNCOV
77
  assert(typeof value === "function", () => new TypeAssertionError("a function", value, target));
×
78
};
79

UNCOV
80
assert.symbol = (value: unknown, target?: string): asserts value is symbol => {
×
81
  assert(typeof value === "symbol", () => new TypeAssertionError("a symbol", value, target));
×
82
};
83

UNCOV
84
assert.object = <K extends string | number | symbol = string, V = unknown>(
×
85
  value: unknown,
86
  target?: string,
87
  { allowArrays } = { allowArrays: true },
×
88
): asserts value is Record<K, V> => {
UNCOV
89
  assert(
×
90
    typeof value === "object" && value !== null && (allowArrays || !Array.isArray(value)),
×
UNCOV
91
    () => new TypeAssertionError("an object", value, target),
×
92
  );
93
};
94

UNCOV
95
assert.undefined = (value: unknown, target?: string): asserts value is undefined => {
×
UNCOV
96
  assert(typeof value === "undefined", () => new TypeAssertionError("undefined", value, target));
×
97
};
98

UNCOV
99
assert.null = (value: unknown, target?: string): asserts value is null => {
×
100
  assert(value === null, () => new TypeAssertionError("null", value, target));
×
101
};
102

UNCOV
103
assert.array = (value: unknown, target?: string): asserts value is Array<unknown> => {
×
UNCOV
104
  assert(Array.isArray(value), () => new TypeAssertionError("an array", value, target));
×
105
};
106

107
/* eslint-disable-next-line @typescript-eslint/ban-types */
UNCOV
108
assert.extends = <T extends Function>(
×
109
  value: unknown,
110
  constructor: T,
111
  target?: string,
112
): asserts value is T & DefaultObject => {
UNCOV
113
  assert.function(value, target);
×
UNCOV
114
  assert(
×
115
    value.prototype instanceof constructor,
116
    () => new TypeAssertionError(`a class extending ${constructor.name}`, value, target),
×
117
  );
118
};
119

UNCOV
120
assert.iterable = (value: unknown, target?: string): asserts value is Iterable<unknown> => {
×
UNCOV
121
  assert.object(value, target);
×
UNCOV
122
  assert(Symbol.iterator in value, () => new TypeAssertionError("iterable", value, target));
×
123
};
124

125
// * Use arg type `value: never` rather than `value: unknown` to get a compile time
126
//   error when e.g. not including if-checks for all enum values.
127
// * Use return type `never` rather than `asserts value is never` to remove the
128
//   need for callers to explicitly throw (i.e. `throw assert.never()`) as a way
129
//   for TS to detect unreachable code.
UNCOV
130
assert.never = (value: never, target?: string): never => {
×
131
  throw new TypeAssertionError("never", value, target);
×
132
};
133

134
// SDK specific
135

UNCOV
136
assert.primaryKey = (value: unknown, target?: string): asserts value is PrimaryKey => {
×
UNCOV
137
  assert(
×
138
    value === null ||
×
139
      typeof value === "number" ||
140
      typeof value === "string" ||
141
      value instanceof BSON.UUID ||
142
      value instanceof BSON.ObjectId,
143
    () => new TypeAssertionError("a primary key", value, target),
×
144
  );
145
};
146

UNCOV
147
assert.open = (realm: Realm) => {
×
UNCOV
148
  assert(!realm.isClosed, "Cannot access realm that has been closed.");
×
149
};
150

UNCOV
151
assert.inTransaction = (realm: Realm, message = "Cannot modify managed objects outside of a write transaction.") => {
×
UNCOV
152
  assert.open(realm);
×
UNCOV
153
  assert(realm.isInTransaction, message);
×
154
};
155

UNCOV
156
assert.outTransaction = (realm: Realm, message = "Expected realm to be outside of a write transaction") => {
×
UNCOV
157
  assert.open(realm);
×
UNCOV
158
  assert(!realm.isInTransaction, message);
×
159
};
160

UNCOV
161
assert.isValid = (obj: binding.Obj, message = "Accessing object which has been invalidated or deleted") => {
×
UNCOV
162
  assert(obj.isValid, message);
×
163
};
164

UNCOV
165
assert.isSameRealm = (realm1: binding.Realm, realm2: binding.Realm, message = "Expected the Realms to be the same") => {
×
UNCOV
166
  assert(realm1.$addr == realm2.$addr, message);
×
167
};
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