Skip to content

Commit

Permalink
feat(select): add select mode
Browse files Browse the repository at this point in the history
  • Loading branch information
GertSallaerts committed Dec 2, 2022
1 parent 8184a10 commit faa2d70
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 9 deletions.
12 changes: 11 additions & 1 deletion src/parse.js
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand All @@ -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 = {};
Expand Down Expand Up @@ -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)
Expand Down
17 changes: 13 additions & 4 deletions src/transformers/select.js
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
Expand Down
8 changes: 4 additions & 4 deletions test/parse.js
Original file line number Diff line number Diff line change
Expand Up @@ -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'
Expand All @@ -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'
Expand Down Expand Up @@ -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'
Expand All @@ -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'
Expand Down

0 comments on commit faa2d70

Please sign in to comment.