Skip to content

Commit

Permalink
move and refactor size function
Browse files Browse the repository at this point in the history
  • Loading branch information
ChromeGG committed Mar 14, 2024
1 parent dd7deb2 commit 0c709cc
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 14 deletions.
12 changes: 12 additions & 0 deletions src/helper.ts
Original file line number Diff line number Diff line change
Expand Up @@ -315,3 +315,15 @@ export const getPosition = (

return ctx.children.OpenBracket[0].startOffset
}

export const size = (arr: unknown) => {
if (isString(arr) || isArray(arr)) {
return arr.length
}

if (isMap(arr)) {
return Object.keys(arr).length
}

throw new CelEvaluationError(`invalid_argument: ${arr}`)
}
15 changes: 1 addition & 14 deletions src/visitor.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,27 +26,14 @@ import {
getPosition,
getResult,
getUnaryResult,
size,
} from './helper.js'
import { CelEvaluationError } from './index.js'

const parserInstance = new CelParser()

const BaseCelVisitor = parserInstance.getBaseCstVisitorConstructor()

const size = (arr: unknown) => {
switch (getCelType(arr)) {
case CelType.list:
case CelType.string:
// @ts-expect-error type assertion error
return arr.length
case CelType.map:
// @ts-expect-error type assertion error
return Object.keys(arr).length
default:
throw new CelEvaluationError(`invalid_argument: ${arr}`)
}
}

export class CelVisitor
extends BaseCelVisitor
implements ICstNodeVisitor<void, unknown>
Expand Down

0 comments on commit 0c709cc

Please sign in to comment.