Skip to content

Commit

Permalink
refactor: remove unused code
Browse files Browse the repository at this point in the history
  • Loading branch information
epoberezkin committed Aug 25, 2020
1 parent 188eadf commit 0567b0e
Show file tree
Hide file tree
Showing 4 changed files with 9 additions and 55 deletions.
20 changes: 8 additions & 12 deletions CUSTOM.md
Original file line number Diff line number Diff line change
Expand Up @@ -307,16 +307,12 @@ There is a number of variables and expressions you can use in the generated (val

There are sevral useful functions you can use in your inline keywords. These functions are available as properties of `it.util` object:

##### .copy(Object obj[, Object target]) -> Object

Clone or extend the object. If one object is passed, it is cloned. If two objects are passed, the second object is extended with the properties of the first.

##### .toHash(Array arr) -> Object

Converts the array of strings to the object where each string becomes the key with the value of `true`.

```javascript
it.util.toHash(["a", "b", "c"]) // { a: true, b: true, c: true }
toHash(["a", "b", "c"]) // { a: true, b: true, c: true }
```

##### .equal(value1, value2) -> Boolean
Expand All @@ -328,18 +324,18 @@ Performs deep equality comparison. This function is used in keywords `enum`, `co
Converts the string that is the key/index to access the property/item to the JavaScript syntax to access the property (either "." notation or "[...]" notation).

```javascript
it.util.getProperty("a") // ".a"
it.util.getProperty("1") // "['1']"
it.util.getProperty("a'b") // "['a\\'b']"
it.util.getProperty(1) // "[1]"
getProperty("a") // ".a"
getProperty("1") // "['1']"
getProperty("a'b") // "['a\\'b']"
getProperty(1) // "[1]"
```

##### .schemaHasRules(Object schema, Object rules) -> String

Determines whether the passed schema has rules that should be validated. This function should be used before calling `it.validate` to compile subschemas.

```javascript
it.util.schemaHasRules(schema, it.RULES.all) // true or false
schemaHasRules(schema, it.RULES.all) // true or false
```

##### .escapeQuotes(String str) -> String
Expand All @@ -351,15 +347,15 @@ Escapes single quotes in the string, so it can be inserted in the generated code
Converts the string to the JavaScript string constant in single quotes (using the escaped string).

```javascript
it.util.toQuotedString("a'b") // "'a\\'b'"
toQuotedString("a'b") // "'a\\'b'"
```

##### .getData(String jsonPointer, Number dataLevel, Array paths) -> String

Returns the validation-time expression to safely access data based on the passed [relative json pointer](https://tools.ietf.org/html/draft-luff-relative-json-pointer-00) (See [examples](https://gist.github.com/geraintluff/5911303)).

```javascript
it.util.getData("2/test/1", it.dataLevel, it.dataPathArr)
getData("2/test/1", it.dataLevel, it.dataPathArr)
// The result depends on the current level
// if it.dataLevel is 3 the result is "data1 && data1.test && data1.test[1]"
```
Expand Down
7 changes: 1 addition & 6 deletions lib/compile/index.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
import CodeGen from "./codegen"
import {toQuotedString} from "./util"
import {quotedString} from "../vocabularies/util"
import {MissingRefError} from "./error_classes"
import validateCode from "./validate"
import {validateKeywordSchema} from "./validate/keyword"
import {ErrorObject, KeywordCompilationResult} from "../types"
Expand All @@ -10,7 +9,6 @@ const equal = require("fast-deep-equal")
const ucs2length = require("./ucs2length")

var resolve = require("./resolve"),
util = require("./util"),
stableStringify = require("fast-json-stable-stringify")

/**
Expand Down Expand Up @@ -128,11 +126,8 @@ function compile(schema, root, localRefs, baseId) {
dataLevel: 0,
data: "data", // TODO get unique name when passed from applicator keywords
gen: new CodeGen(),
MissingRefError,
RULES,
RULES, // TODO refactor - it is available on the instance
validateCode,
util, // TODO remove to imports
resolve, // TODO remove to imports
resolveRef, // TODO remove to imports
usePattern, // TODO remove to imports
useDefault, // TODO remove to imports
Expand Down
33 changes: 0 additions & 33 deletions lib/compile/util.ts
Original file line number Diff line number Diff line change
@@ -1,23 +1,3 @@
// TODO switch to exports - below are used in dot templates
module.exports = {
checkDataType,
checkDataTypes,
toHash,
escapeQuotes,
varOccurrences,
varReplace,
schemaHasRules,
schemaHasRulesExcept,
schemaUnknownRules,
toQuotedString,
getPathExpr,
getPath,
getData,
getProperty,
unescapeFragment,
escapeFragment,
}

export function checkDataType(
dataType: string,
data: string,
Expand Down Expand Up @@ -96,19 +76,6 @@ export function escapeQuotes(str: string): string {
.replace(/\t/g, "\\t")
}

export function varOccurrences(str: string, dataVar: string): number {
dataVar += "[^0-9]"
/* eslint-disable @typescript-eslint/prefer-regexp-exec */
const matches = str.match(new RegExp(dataVar, "g"))
return matches ? matches.length : 0
}

export function varReplace(str: string, dataVar: string, expr: string): string {
dataVar += "([^0-9])"
expr = expr.replace(/\$/g, "$$$$")
return str.replace(new RegExp(dataVar, "g"), expr + "$1")
}

// TODO rules, schema?
export function schemaHasRules(schema: object | boolean, rules: object): boolean | undefined {
if (typeof schema == "boolean") return !schema
Expand Down
4 changes: 0 additions & 4 deletions lib/types.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
import Cache from "./cache"
import CodeGen from "./compile/codegen"
import {ValidationRules} from "./compile/rules"
import {MissingRefError} from "./compile/error_classes"
import {ResolvedRef} from "./compile"

export interface Options {
Expand Down Expand Up @@ -132,16 +131,13 @@ export interface CompilationContext {
useDefault: (value: any) => string
customRules: KeywordCompilationResult[]
validateKeywordSchema: (it: CompilationContext, keyword: string, def: KeywordDefinition) => void // TODO remove
util: any // TODO
self: any // TODO
RULES: ValidationRules
logger: Logger // TODO ?
isTop?: boolean // TODO ?
root: SchemaRoot // TODO ?
rootId: string // TODO ?
topSchemaRef: string
MissingRefError: typeof MissingRefError
resolve: any
resolveRef: (...args: any[]) => ResolvedRef | void
}

Expand Down

0 comments on commit 0567b0e

Please sign in to comment.