A RetroSearch Logo

Home - News ( United States | United Kingdom | Italy | Germany ) - Football scores

Search Query:

Showing content from https://github.com/jsonquerylang/jsonquery/commit/7cea93ed3206ebd90aaef3a23082c38094243c8f below:

a minor performance improvement in `parse` · jsonquerylang/jsonquery@7cea93e · GitHub

File tree Expand file treeCollapse file tree 2 files changed

+10

-8

lines changed

Filter options

Expand file treeCollapse file tree 2 files changed

+10

-8

lines changed Original file line number Diff line number Diff line change

@@ -26,6 +26,9 @@ import type { JSONQuery, JSONQueryParseOptions } from './types'

26 26

* // ]

27 27

*/

28 28

export function parse(query: string, options?: JSONQueryParseOptions): JSONQuery {

29 +

const allOperators = { ...operators, ...options?.operators }

30 +

const sortedOperatorNames = Object.keys(allOperators).sort((a, b) => b.length - a.length)

31 + 29 32

const parsePipe = () => {

30 33

skipWhitespace()

31 34

const first = parseOperator()

@@ -48,19 +51,18 @@ export function parse(query: string, options?: JSONQueryParseOptions): JSONQuery

48 51

}

49 52 50 53

const parseOperator = () => {

51 -

const allOperators = { ...operators, ...options?.operators }

52 54 53 -

const left = parseParentheses()

55 +

const left = parseParenthesis()

54 56 55 57

skipWhitespace()

56 58 57 59

// we sort the operators from longest to shortest, so we first handle "<=" and next "<"

58 -

for (const name of Object.keys(allOperators).sort((a, b) => b.length - a.length)) {

60 +

for (const name of sortedOperatorNames) {

59 61

const op = allOperators[name]

60 62

if (query.substring(i, i + op.length) === op) {

61 63

i += op.length

62 64

skipWhitespace()

63 -

const right = parseParentheses()

65 +

const right = parseParenthesis()

64 66 65 67

return [name, left, right]

66 68

}

@@ -69,7 +71,7 @@ export function parse(query: string, options?: JSONQueryParseOptions): JSONQuery

69 71

return left

70 72

}

71 73 72 -

const parseParentheses = () => {

74 +

const parseParenthesis = () => {

73 75

if (query[i] === '(') {

74 76

i++

75 77

const inner = parsePipe()

@@ -81,9 +83,9 @@ export function parse(query: string, options?: JSONQueryParseOptions): JSONQuery

81 83

}

82 84 83 85

const parseProperty = () => {

84 -

const props = []

85 - 86 86

if (query[i] === '.') {

87 +

const props = []

88 + 87 89

while (query[i] === '.') {

88 90

i++

89 91 Original file line number Diff line number Diff line change

@@ -76,7 +76,7 @@ export const stringify = (query: JSONQuery, options?: JSONQueryStringifyOptions)

76 76

const childIndent = args.length === 1 ? indent : indent + space

77 77

const argsStr = args.map((arg) => _stringify(arg, childIndent))

78 78

return args.length === 1 && argsStr[0][0] === '('

79 -

? `${name}${argsStr}`

79 +

? `${name}${argsStr[0]}`

80 80

: join(

81 81

argsStr,

82 82

[`${name}(`, ', ', ')'],

You can’t perform that action at this time.


RetroSearch is an open source project built by @garambo | Open a GitHub Issue

Search and Browse the WWW like it's 1997 | Search results from DuckDuckGo

HTML: 3.2 | Encoding: UTF-8 | Version: 0.7.4