Skip to content

Commit

Permalink
Merge branch 'master' of https://github.com/vivocha/arrest
Browse files Browse the repository at this point in the history
  • Loading branch information
0xfede committed Nov 26, 2021
2 parents 13e090d + f968c81 commit 3b58a9f
Show file tree
Hide file tree
Showing 5 changed files with 43 additions and 6 deletions.
6 changes: 6 additions & 0 deletions src/defaults.ts
Original file line number Diff line number Diff line change
Expand Up @@ -169,6 +169,12 @@ export const DEFAULT_DOCUMENT: OpenAPIV3.Document = {
minLength: 1,
maxLength: 1,
},
decimal: {
description: 'decimal separator character (default: ".")',
type: 'string',
minLength: 1,
maxLength: 1,
},
eol: {
description: 'End-of-line character sequence',
type: 'string',
Expand Down
8 changes: 6 additions & 2 deletions src/mongo/util.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,10 @@ import { rulesToQuery } from '@casl/ability/extra';
import { JSONPatch } from 'jsonref';
import { ObjectId } from 'mongodb';

function haveKeysInCommon(a: any, b: any): boolean {
return !!Object.keys(a).find((k) => (typeof b[k] !== 'undefined' ? k : undefined));
}

export function addConstraint(query: any, constraint: any): any {
if (!query) {
query = {};
Expand All @@ -23,13 +27,13 @@ export function addConstraint(query: any, constraint: any): any {
query = { $and: [query, constraint] };
} else if (query.$and && Object.keys(constraint).length > 0) {
const lastCond = query.$and[query.$and.length - 1];
if (lastCond && !(lastCond.$or && constraint.$or) && !(lastCond.$and && constraint.$and)) {
if (lastCond && !(lastCond.$or && constraint.$or) && !(lastCond.$and && constraint.$and) && !haveKeysInCommon(lastCond, constraint)) {
Object.assign(lastCond, constraint);
} else {
query.$and.push(constraint);
}
} else {
if (Object.keys(constraint).find((k) => (typeof query[k] !== 'undefined' ? k : undefined))) {
if (haveKeysInCommon(query, constraint)) {
query = { $and: [query, constraint] };
} else {
Object.assign(query, constraint);
Expand Down
8 changes: 7 additions & 1 deletion src/util.ts
Original file line number Diff line number Diff line change
Expand Up @@ -338,6 +338,7 @@ export interface CSVOptions {
fields: string[] | { [key: string]: string };
unwind?: string;
separator?: string;
decimal?: string;
quotes?: boolean;
escape?: string;
eol?: string;
Expand Down Expand Up @@ -378,7 +379,12 @@ export function toCSV(data: any[], options: CSVOptions): string {
const l: string[] = [];
// TODO optimize with a transversal map
for (let k in fieldMap) {
l.push(dot.get(item, k) || '');
const value: any = dot.get(item, k);
if (options.decimal && typeof value === 'number') {
l.push(value.toString().replace('.', options.decimal));
} else {
l.push((value || '').toString());
}
}
out.push(l);
});
Expand Down
1 change: 1 addition & 0 deletions test/ts/mongo.util.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,7 @@ describe('util', function () {
});
it('should handle multiple constraints on a property producing an $and', function () {
addConstraint({ a: 1 }, { a: 2 }).should.deep.equal({ $and: [{ a: 1 }, { a: 2 }] });
addConstraint({ $and: [{ a: 1 }, { a: 2 }] }, { a: 3 }).should.deep.equal({ $and: [{ a: 1 }, { a: 2 }, { a: 3 }] });
});
});
});
Expand Down
26 changes: 23 additions & 3 deletions test/ts/pipeline.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -206,7 +206,7 @@ XXX,,YYY`

it('should return the requested top level fields as csv, with quoted fields', function () {
csv_options = {
fields: ['a', 'b'],
fields: ['a', 'b', 'c'],
quotes: true,
};
csv_data = [
Expand All @@ -219,8 +219,8 @@ XXX,,YYY`
.expect('Content-Type', /text\/csv/)
.then(({ text: data }) => {
data.should.equal(
`"AAA","BBB"
"XXX","YYY"`
`"AAA","BBB","1"
"XXX","YYY","1"`
);
});
});
Expand Down Expand Up @@ -287,6 +287,26 @@ XXX,,YYY`
});
});

it('should return the requested top level fields as csv, with custom decimal separator', function () {
csv_options = {
fields: ['a', 'b', 'c'],
decimal: ',',
quotes: true,
};
csv_data = [
{ a: 'AAA', b: 'BBB', c: 1.5, d: true },
{ a: 'XXX', b: 'YYY', c: 1.9, d: false },
];
return request
.get('/tests')
.expect(200)
.expect('Content-Type', /text\/csv/)
.then(({ text: data }) => {
data.should.equal(`"AAA","BBB","1,5"
"XXX","YYY","1,9"`);
});
});

it('should return the requested top level fields as csv, with header', function () {
csv_options = {
fields: ['a', 'b'],
Expand Down

0 comments on commit 3b58a9f

Please sign in to comment.