Skip to content
This repository has been archived by the owner on Nov 9, 2021. It is now read-only.

Add GetFieldBlock methods to remove fields from final result set #344

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
38 changes: 38 additions & 0 deletions dist/squel-basic.js
Original file line number Diff line number Diff line change
Expand Up @@ -1571,6 +1571,44 @@ function _buildSquel() {
options: options
});
}

/**
# Remove the given field from the final result set.
#
*/

}, {
key: 'removeField',
value: function removeField(field) {
field = this._sanitizeField(field);

// search by alias
var index = this._fields.findIndex(function (f) {
return f.alias === field;
});

if (index === -1) {
// search by name
index = this._fields.findIndex(function (f) {
return f.name === field;
});
}

if (index !== -1) {
this._fields.splice(index, 1);
}
}

/**
# Remove all fields from the final result set.
#
*/

}, {
key: 'removeAllFields',
value: function removeAllFields() {
this._fields = [];
}
}, {
key: '_toParamString',
value: function _toParamString() {
Expand Down
4 changes: 2 additions & 2 deletions dist/squel-basic.min.js

Large diffs are not rendered by default.

38 changes: 38 additions & 0 deletions dist/squel.js
Original file line number Diff line number Diff line change
Expand Up @@ -1571,6 +1571,44 @@ function _buildSquel() {
options: options
});
}

/**
# Remove the given field from the final result set.
#
*/

}, {
key: 'removeField',
value: function removeField(field) {
field = this._sanitizeField(field);

// search by alias
var index = this._fields.findIndex(function (f) {
return f.alias === field;
});

if (index === -1) {
// search by name
index = this._fields.findIndex(function (f) {
return f.name === field;
});
}

if (index !== -1) {
this._fields.splice(index, 1);
}
}

/**
# Remove all fields from the final result set.
#
*/

}, {
key: 'removeAllFields',
value: function removeAllFields() {
this._fields = [];
}
}, {
key: '_toParamString',
value: function _toParamString() {
Expand Down
4 changes: 2 additions & 2 deletions dist/squel.min.js

Large diffs are not rendered by default.

31 changes: 31 additions & 0 deletions src/core.js
Original file line number Diff line number Diff line change
Expand Up @@ -1209,6 +1209,37 @@ function _buildSquel(flavour = null) {
});
}

/**
# Remove the given field from the final result set.
#
*/
removeField (field) {
field = this._sanitizeField(field);

// search by alias
let index = this._fields.findIndex((f) => {
return f.alias === field;
});

if (index === -1) {
// search by name
index = this._fields.findIndex((f) => {
return f.name === field;
});
}

if (index !== -1) {
this._fields.splice(index, 1);
}
}

/**
# Remove all fields from the final result set.
#
*/
removeAllFields () {
this._fields = [];
}

_toParamString (options = {}) {
let { queryBuilder, buildParameterized } = options;
Expand Down
29 changes: 29 additions & 0 deletions test/blocks.test.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -499,6 +499,35 @@ test['Blocks'] =

assert.same expected, @inst._fields

'remove inputs': ->
@inst.field('field1')
@inst.field('field2', 'alias2')
@inst.field('field1', 'alias1')

@inst.removeField('alias1')
@inst.removeField('field2')

expected = [
{
name: 'field1',
alias: null
options: {},
}
]

assert.same expected, @inst._fields

'remove all inputs': ->
@inst.field('field1')
@inst.field('field2', 'alias2')
@inst.field('field1', 'alias1')

@inst.removeAllFields()

expected = []

assert.same expected, @inst._fields

'field() - discard duplicates':
'saves inputs': ->
@inst.field('field1')
Expand Down