Skip to content

Commit

Permalink
add useDotAccessOperator #44
Browse files Browse the repository at this point in the history
  • Loading branch information
cshaa committed Sep 22, 2021
1 parent 7cdaea0 commit 56df51f
Show file tree
Hide file tree
Showing 8 changed files with 249 additions and 16 deletions.
47 changes: 44 additions & 3 deletions dist/browser/filtrex.js
Original file line number Diff line number Diff line change
Expand Up @@ -2261,6 +2261,46 @@ var filtrex = (function (exports) {
parser.yy = Object.create(std);


/**
* A custom prop function which treats dots inside a symbol
* as property accessors. If you want to use the `foo.bar`
* syntax to access properties instead of the default
* `bar of foo`, you can use this function using the following
* code:
* ```
* import {
* compileExpression,
* useDotAccessOperator
* } from 'filtrex'
*
* const expr = "foo.bar"
*
* const fn = compileExpression(expr, {
* customProp: useDotAccessOperator
* });
*
* fn({ foo: { bar: 42 } }) // → 42
* ```
*/
function useDotAccessOperator(name, get, obj, type) {
// ignore dots inside escaped symbol
if (type === 'single-quoted')
return get(name)


const parts = name.split('.');

for (const propertyName of parts) {
if (hasOwnProperty(obj ?? {}, propertyName)) {
obj = obj[propertyName];
} else {
throw new UnknownPropertyError(propertyName)
}
}

return obj
}


/**
* A simple, safe, JavaScript expression engine, allowing end-users to enter arbitrary expressions without p0wning you.
Expand Down Expand Up @@ -2394,7 +2434,7 @@ var filtrex = (function (exports) {

// Metaprogramming functions

function nakedProp(name, obj) {
function nakedProp(name, obj, type) {
if (hasOwnProperty(obj ?? {}, name))
return obj[name]

Expand All @@ -2411,7 +2451,7 @@ var filtrex = (function (exports) {
}

if (typeof customProp === 'function') {
nakedProp = (name, obj) => customProp(name, safeGetter(obj), obj);
nakedProp = (name, obj, type) => customProp(name, safeGetter(obj), obj, type);
}

function createCall(fns) {
Expand All @@ -2427,7 +2467,7 @@ var filtrex = (function (exports) {
if (type === 'unescaped' && hasOwnProperty(constants, name))
return constants[name]

return nakedProp(name, obj)
return nakedProp(name, obj, type)
}


Expand All @@ -2448,6 +2488,7 @@ var filtrex = (function (exports) {
}

exports.compileExpression = compileExpression;
exports.useDotAccessOperator = useDotAccessOperator;

Object.defineProperty(exports, '__esModule', { value: true });

Expand Down
2 changes: 1 addition & 1 deletion dist/browser/filtrex.min.js

Large diffs are not rendered by default.

47 changes: 44 additions & 3 deletions dist/cjs/filtrex.js
Original file line number Diff line number Diff line change
Expand Up @@ -2262,6 +2262,46 @@ const std =
parser.yy = Object.create(std);


/**
* A custom prop function which treats dots inside a symbol
* as property accessors. If you want to use the `foo.bar`
* syntax to access properties instead of the default
* `bar of foo`, you can use this function using the following
* code:
* ```
* import {
* compileExpression,
* useDotAccessOperator
* } from 'filtrex'
*
* const expr = "foo.bar"
*
* const fn = compileExpression(expr, {
* customProp: useDotAccessOperator
* });
*
* fn({ foo: { bar: 42 } }) // → 42
* ```
*/
function useDotAccessOperator(name, get, obj, type) {
// ignore dots inside escaped symbol
if (type === 'single-quoted')
return get(name)


const parts = name.split('.');

for (const propertyName of parts) {
if (hasOwnProperty(obj ?? {}, propertyName)) {
obj = obj[propertyName];
} else {
throw new UnknownPropertyError(propertyName)
}
}

return obj
}


/**
* A simple, safe, JavaScript expression engine, allowing end-users to enter arbitrary expressions without p0wning you.
Expand Down Expand Up @@ -2395,7 +2435,7 @@ function compileExpression(expression, options) {

// Metaprogramming functions

function nakedProp(name, obj) {
function nakedProp(name, obj, type) {
if (hasOwnProperty(obj ?? {}, name))
return obj[name]

Expand All @@ -2412,7 +2452,7 @@ function compileExpression(expression, options) {
}

if (typeof customProp === 'function') {
nakedProp = (name, obj) => customProp(name, safeGetter(obj), obj);
nakedProp = (name, obj, type) => customProp(name, safeGetter(obj), obj, type);
}

function createCall(fns) {
Expand All @@ -2428,7 +2468,7 @@ function compileExpression(expression, options) {
if (type === 'unescaped' && hasOwnProperty(constants, name))
return constants[name]

return nakedProp(name, obj)
return nakedProp(name, obj, type)
}


Expand All @@ -2449,3 +2489,4 @@ function compileExpression(expression, options) {
}

exports.compileExpression = compileExpression;
exports.useDotAccessOperator = useDotAccessOperator;
31 changes: 30 additions & 1 deletion dist/esm/filtrex.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -106,7 +106,8 @@ export interface Options
customProp?: (
name: string,
get: (name: string) => any,
object: any
object: any,
type: 'unescaped' | 'single-quoted'
) => any

/**
Expand Down Expand Up @@ -134,3 +135,31 @@ export interface Operators {

'~='?: (a: any, b: any) => boolean
}

/**
* A custom prop function which treats dots inside a symbol
* as property accessors. If you want to use the `foo.bar`
* syntax to access properties instead of the default
* `bar of foo`, you can use this function using the following
* code:
* ```
* import {
* compileExpression,
* useDotAccessOperator
* } from 'filtrex'
*
* const expr = "foo.bar"
*
* const fn = compileExpression(expr, {
* customProp: useDotAccessOperator
* });
*
* fn({ foo: { bar: 42 } }) // → 42
* ```
*/
export function useDotAccessOperator(
name: string,
get: (name: string) => any,
object: any,
type: 'unescaped' | 'single-quoted'
)
46 changes: 43 additions & 3 deletions dist/esm/filtrex.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -118,6 +118,46 @@ const std =
parser.yy = Object.create(std)


/**
* A custom prop function which treats dots inside a symbol
* as property accessors. If you want to use the `foo.bar`
* syntax to access properties instead of the default
* `bar of foo`, you can use this function using the following
* code:
* ```
* import {
* compileExpression,
* useDotAccessOperator
* } from 'filtrex'
*
* const expr = "foo.bar"
*
* const fn = compileExpression(expr, {
* customProp: useDotAccessOperator
* });
*
* fn({ foo: { bar: 42 } }) // → 42
* ```
*/
export function useDotAccessOperator(name, get, obj, type) {
// ignore dots inside escaped symbol
if (type === 'single-quoted')
return get(name)


const parts = name.split('.')

for (const propertyName of parts) {
if (hasOwnProperty(obj ?? {}, propertyName)) {
obj = obj[propertyName]
} else {
throw new UnknownPropertyError(propertyName)
}
}

return obj
}


/**
* A simple, safe, JavaScript expression engine, allowing end-users to enter arbitrary expressions without p0wning you.
Expand Down Expand Up @@ -251,7 +291,7 @@ export function compileExpression(expression, options) {

// Metaprogramming functions

function nakedProp(name, obj) {
function nakedProp(name, obj, type) {
if (hasOwnProperty(obj ?? {}, name))
return obj[name]

Expand All @@ -268,7 +308,7 @@ export function compileExpression(expression, options) {
}

if (typeof customProp === 'function') {
nakedProp = (name, obj) => customProp(name, safeGetter(obj), obj)
nakedProp = (name, obj, type) => customProp(name, safeGetter(obj), obj, type)
}

function createCall(fns) {
Expand All @@ -284,7 +324,7 @@ export function compileExpression(expression, options) {
if (type === 'unescaped' && hasOwnProperty(constants, name))
return constants[name]

return nakedProp(name, obj)
return nakedProp(name, obj, type)
}


Expand Down
31 changes: 30 additions & 1 deletion src/filtrex.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -106,7 +106,8 @@ export interface Options
customProp?: (
name: string,
get: (name: string) => any,
object: any
object: any,
type: 'unescaped' | 'single-quoted'
) => any

/**
Expand Down Expand Up @@ -134,3 +135,31 @@ export interface Operators {

'~='?: (a: any, b: any) => boolean
}

/**
* A custom prop function which treats dots inside a symbol
* as property accessors. If you want to use the `foo.bar`
* syntax to access properties instead of the default
* `bar of foo`, you can use this function using the following
* code:
* ```
* import {
* compileExpression,
* useDotAccessOperator
* } from 'filtrex'
*
* const expr = "foo.bar"
*
* const fn = compileExpression(expr, {
* customProp: useDotAccessOperator
* });
*
* fn({ foo: { bar: 42 } }) // → 42
* ```
*/
export function useDotAccessOperator(
name: string,
get: (name: string) => any,
object: any,
type: 'unescaped' | 'single-quoted'
)
46 changes: 43 additions & 3 deletions src/filtrex.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -118,6 +118,46 @@ const std =
parser.yy = Object.create(std)


/**
* A custom prop function which treats dots inside a symbol
* as property accessors. If you want to use the `foo.bar`
* syntax to access properties instead of the default
* `bar of foo`, you can use this function using the following
* code:
* ```
* import {
* compileExpression,
* useDotAccessOperator
* } from 'filtrex'
*
* const expr = "foo.bar"
*
* const fn = compileExpression(expr, {
* customProp: useDotAccessOperator
* });
*
* fn({ foo: { bar: 42 } }) // → 42
* ```
*/
export function useDotAccessOperator(name, get, obj, type) {
// ignore dots inside escaped symbol
if (type === 'single-quoted')
return get(name)


const parts = name.split('.')

for (const propertyName of parts) {
if (hasOwnProperty(obj ?? {}, propertyName)) {
obj = obj[propertyName]
} else {
throw new UnknownPropertyError(propertyName)
}
}

return obj
}


/**
* A simple, safe, JavaScript expression engine, allowing end-users to enter arbitrary expressions without p0wning you.
Expand Down Expand Up @@ -251,7 +291,7 @@ export function compileExpression(expression, options) {

// Metaprogramming functions

function nakedProp(name, obj) {
function nakedProp(name, obj, type) {
if (hasOwnProperty(obj ?? {}, name))
return obj[name]

Expand All @@ -268,7 +308,7 @@ export function compileExpression(expression, options) {
}

if (typeof customProp === 'function') {
nakedProp = (name, obj) => customProp(name, safeGetter(obj), obj)
nakedProp = (name, obj, type) => customProp(name, safeGetter(obj), obj, type)
}

function createCall(fns) {
Expand All @@ -284,7 +324,7 @@ export function compileExpression(expression, options) {
if (type === 'unescaped' && hasOwnProperty(constants, name))
return constants[name]

return nakedProp(name, obj)
return nakedProp(name, obj, type)
}


Expand Down
Loading

0 comments on commit 56df51f

Please sign in to comment.