Skip to content

fix: return columns as well (#785) #786

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Jan 7, 2025
Merged
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
6 changes: 5 additions & 1 deletion src/utils/json-filter.js
Original file line number Diff line number Diff line change
Expand Up @@ -36,12 +36,16 @@ export default function jsonFilter(state, res, query) {
function filter(dataStruct) {
const len = Math.min(limit, dataStruct.data.length - offset);
const filtered = dataStruct.data.slice(offset, offset + len);
return {
const ret = {
total: dataStruct.total,
offset,
limit: filtered.length,
data: filtered,
};
if (dataStruct.columns) {
ret.columns = dataStruct.columns;
}
return ret;
}

const { data } = state.content;
Expand Down
12 changes: 12 additions & 0 deletions test/json-pipe.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,7 @@ describe('JSON Pipe Test', () => {
let TEST_DATA;
let TEST_SINGLE_SHEET;
let TEST_MULTI_SHEET;
const TEST_COLUMNS = ['col0', 'col1', 'col2', 'col3'];

before(async () => {
TEST_DATA = JSON.parse(await readFile(resolve(__testdir, 'fixtures', 'json', 'test-data.json'), 'utf-8'));
Expand All @@ -75,6 +76,7 @@ describe('JSON Pipe Test', () => {
limit: TEST_DATA.length,
total: TEST_DATA.length,
data: TEST_DATA,
columns: TEST_COLUMNS,
});
TEST_MULTI_SHEET = (names = ['foo', 'bar']) => JSON.stringify(
{
Expand All @@ -84,6 +86,7 @@ describe('JSON Pipe Test', () => {
limit: TEST_DATA.length,
total: TEST_DATA.length,
data: TEST_DATA,
columns: TEST_COLUMNS,
}])),
),
':names': names,
Expand Down Expand Up @@ -139,6 +142,7 @@ describe('JSON Pipe Test', () => {
offset: 5,
limit: 10,
total: TEST_DATA.length,
columns: TEST_COLUMNS,
data: TEST_DATA.slice(5, 15),
});
assert.deepStrictEqual(Object.fromEntries(resp.headers.entries()), {
Expand All @@ -163,6 +167,7 @@ describe('JSON Pipe Test', () => {
offset: 5,
limit: 10,
total: TEST_DATA.length,
columns: TEST_COLUMNS,
data: TEST_DATA.slice(5, 15),
});
assert.deepStrictEqual(Object.fromEntries(resp.headers.entries()), {
Expand Down Expand Up @@ -200,6 +205,7 @@ describe('JSON Pipe Test', () => {
offset: 5,
limit: 10,
total: TEST_DATA.length,
columns: TEST_COLUMNS,
data: TEST_DATA.slice(5, 15),
});
const headers = Object.fromEntries(resp.headers.entries());
Expand Down Expand Up @@ -307,6 +313,7 @@ describe('JSON Pipe Test', () => {
offset: 5,
limit: 10,
total: TEST_DATA.length,
columns: TEST_COLUMNS,
data: TEST_DATA.slice(5, 15),
});
const headers = Object.fromEntries(resp.headers.entries());
Expand Down Expand Up @@ -339,6 +346,7 @@ describe('JSON Pipe Test', () => {
offset: 5,
limit: 10,
total: TEST_DATA.length,
columns: TEST_COLUMNS,
data: TEST_DATA.slice(5, 15),
});
const headers = Object.fromEntries(resp.headers.entries());
Expand Down Expand Up @@ -381,6 +389,7 @@ describe('JSON Pipe Test', () => {
offset: 5,
limit: 10,
total: TEST_DATA.length,
columns: TEST_COLUMNS,
data: TEST_DATA.slice(5, 15),
});
assert.deepStrictEqual(Object.fromEntries(resp.headers.entries()), {
Expand Down Expand Up @@ -663,12 +672,14 @@ describe('JSON Pipe Test', () => {
offset: 0,
limit: TEST_DATA.length,
total: TEST_DATA.length,
columns: TEST_COLUMNS,
data: TEST_DATA,
},
bar: {
offset: 0,
limit: TEST_DATA.length,
total: TEST_DATA.length,
columns: TEST_COLUMNS,
data: TEST_DATA,
},
});
Expand Down Expand Up @@ -698,6 +709,7 @@ describe('JSON Pipe Test', () => {
offset: 0,
limit: TEST_DATA.length,
total: TEST_DATA.length,
columns: TEST_COLUMNS,
data: TEST_DATA,
},
});
Expand Down
Loading