diff --git a/src/parse.js b/src/parse.js index 88f6286..94bc98a 100644 --- a/src/parse.js +++ b/src/parse.js @@ -5,10 +5,15 @@ const _get = require('lodash/get'); const isString = require('lodash/isString'); const CustomTransformer = require('./transformers/custom'); +const OPT_DIRECTION = 'direction'; const DIRECTION_ANY = 'ANY'; const DIRECTION_PARSE = 'PARSE'; const DIRECTION_REVERSE = 'REVERSE'; +const OPT_SELECT_MODE = 'select-mode'; +const SELECT_MODE_JS = 'js'; +const SELECT_MODE_LODASH = 'lodash'; + function Parse(path, options = {}) { if (!(this instanceof Parse)) return new Parse(path, options); @@ -22,10 +27,15 @@ function Parse(path, options = {}) { return this; } +Parse.OPT_DIRECTION = OPT_DIRECTION; Parse.DIRECTION_ANY = DIRECTION_ANY; Parse.DIRECTION_PARSE = DIRECTION_PARSE; Parse.DIRECTION_REVERSE = DIRECTION_REVERSE; +Parse.OPT_SELECT_MODE = OPT_SELECT_MODE; +Parse.SELECT_MODE_JS = SELECT_MODE_JS; +Parse.SELECT_MODE_LODASH = SELECT_MODE_LODASH; + module.exports = Parse; Parse.options = {}; @@ -71,7 +81,7 @@ Parse.prototype.chain = function(configurator) { Parse.prototype.isDirectionEnabled = function(direction) { direction = direction.toUpperCase(); - const configuredDirection = (this.getOption('direction') || DIRECTION_ANY); + const configuredDirection = (this.getOption(OPT_DIRECTION) || DIRECTION_ANY); const enabledDirection = configuredDirection.toUpperCase(); if (DIRECTION_ANY == enabledDirection) diff --git a/src/transformers/select.js b/src/transformers/select.js index e42a174..38caa2f 100644 --- a/src/transformers/select.js +++ b/src/transformers/select.js @@ -2,11 +2,20 @@ const _get = require('lodash/get'); const _set = require('lodash/set'); +const Parse = require('../parse'); -function SelectTransformer(path) { - if( !(this instanceof SelectTransformer) ) { - return this.transform(new SelectTransformer(path)); - } +function SelectTransformer(path, { mode } = {}) { + if (this instanceof Parse && !mode) + mode = this.getOption(Parse.OPT_SELECT_MODE); + + if (!mode) + mode = Parse.SELECT_MODE_LODASH; + + if (!(this instanceof SelectTransformer)) + return this.transform(new SelectTransformer(path, { mode })); + + if (mode === Parse.SELECT_MODE_JS) + path = `["${path}"]`; this._path = path; } diff --git a/test/parse.js b/test/parse.js index 4557912..e917f34 100644 --- a/test/parse.js +++ b/test/parse.js @@ -154,7 +154,7 @@ describe('Parse', function() { it('Should return original when parse is disabled', function() { const instance = new Parse('test'); - instance.setOption('direction', Parse.DIRECTION_REVERSE); + instance.setOption(Parse.OPT_DIRECTION, Parse.DIRECTION_REVERSE); const obj = { test: 'abc' @@ -167,7 +167,7 @@ describe('Parse', function() { it('Should parse when reverse is disabled', function() { const instance = new Parse('test'); - instance.setOption('direction', Parse.DIRECTION_PARSE); + instance.setOption(Parse.OPT_DIRECTION, Parse.DIRECTION_PARSE); const obj = { test: 'abc' @@ -211,7 +211,7 @@ describe('Parse', function() { it('Should return original when reverse is disabled', function() { const instance = new Parse('test'); - instance.setOption('direction', Parse.DIRECTION_PARSE); + instance.setOption(Parse.OPT_DIRECTION, Parse.DIRECTION_PARSE); const obj = { test: 'abc' @@ -222,7 +222,7 @@ describe('Parse', function() { it('Should reverse when parse is disabled', function() { const instance = new Parse('test'); - instance.setOption('direction', Parse.DIRECTION_REVERSE); + instance.setOption(Parse.OPT_DIRECTION, Parse.DIRECTION_REVERSE); const obj = { test: 'abc'