source: imaps-frontend/node_modules/ajv-keywords/src/definitions/instanceof.ts

main
Last change on this file was 79a0317, checked in by stefan toskovski <stefantoska84@…>, 4 days ago

F4 Finalna Verzija

  • Property mode set to 100644
File size: 1.5 KB
Line 
1import type {FuncKeywordDefinition} from "ajv"
2
3type Constructor = new (...args: any[]) => any
4
5const CONSTRUCTORS: Record<string, Constructor | undefined> = {
6 Object,
7 Array,
8 Function,
9 Number,
10 String,
11 Date,
12 RegExp,
13}
14
15/* istanbul ignore else */
16if (typeof Buffer != "undefined") CONSTRUCTORS.Buffer = Buffer
17
18/* istanbul ignore else */
19if (typeof Promise != "undefined") CONSTRUCTORS.Promise = Promise
20
21const getDef: (() => FuncKeywordDefinition) & {
22 CONSTRUCTORS: typeof CONSTRUCTORS
23} = Object.assign(_getDef, {CONSTRUCTORS})
24
25function _getDef(): FuncKeywordDefinition {
26 return {
27 keyword: "instanceof",
28 schemaType: ["string", "array"],
29 compile(schema: string | string[]) {
30 if (typeof schema == "string") {
31 const C = getConstructor(schema)
32 return (data) => data instanceof C
33 }
34
35 if (Array.isArray(schema)) {
36 const constructors = schema.map(getConstructor)
37 return (data) => {
38 for (const C of constructors) {
39 if (data instanceof C) return true
40 }
41 return false
42 }
43 }
44
45 /* istanbul ignore next */
46 throw new Error("ajv implementation error")
47 },
48 metaSchema: {
49 anyOf: [{type: "string"}, {type: "array", items: {type: "string"}}],
50 },
51 }
52}
53
54function getConstructor(c: string): Constructor {
55 const C = CONSTRUCTORS[c]
56 if (C) return C
57 throw new Error(`invalid "instanceof" keyword value ${c}`)
58}
59
60export default getDef
61module.exports = getDef
Note: See TracBrowser for help on using the repository browser.