Skip to content

Commit

Permalink
Adding ops: $nin, $substr related and $bit related (#24)
Browse files Browse the repository at this point in the history
* Adding ops: $bitAnd, $bitNot, $bitOr and $bitXor

* Adding ops: $nin, $$slice, $substr..

* Use yarn 1.22.22 like ci
  • Loading branch information
speedytwenty authored Sep 11, 2024
1 parent 44bacc8 commit 595f95d
Show file tree
Hide file tree
Showing 4 changed files with 4,164 additions and 6,329 deletions.
98 changes: 98 additions & 0 deletions index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,8 @@ type StringExpression = ObjectExpression | Array<any> | string;
*/
type DateExpression = ObjectExpression | Date | string;

type ArrayOfExpressions = Array<ObjectExpression>;

type Timestamp = number;

// always two
Expand Down Expand Up @@ -1479,6 +1481,65 @@ type BinarySizeOperator = {
*/
const $binarySize = se('$binarySize');

/**
* Returns the result of a bitwise and operation on an array of int or long
* values.
* @category Operators
* @function
* @param {...ObjectExpression} expressions int or long values.
* @see {@link https://www.mongodb.com/docs/manual/reference/operator/aggregation/bitAnd/|MongoDB reference}
* for $bitAnd
* @example
* $bitAnd('$myInt', '$myLong');
* // returns
* { $bitAnd: ['$myInt', '$myLong'] }
*/
const $bitAnd = ptafaa('$bitAnd');

/**
* Returns the result of a bitwise not operation on a single int or long value.
* @category Operators
* @function
* @param {...ObjectExpression} expressions int or long values.
* @see {@link https://www.mongodb.com/docs/manual/reference/operator/aggregation/bitNot/|MongoDB reference}
* for $bitAny
* @example
* $bitNot('$x');
* // returns
* { $bitNot: '$x' }
*/
const $bitNot = se('$bitNot');

/**
* Returns the result of a bitwise or operation on an array of int and long
* values.
* @category Operators
* @function
* @param {...ObjectExpression} expressions int or long values.
* @see {@link https://www.mongodb.com/docs/manual/reference/operator/aggregation/bitOr/|MongoDB reference}
* for $bitOr
* @example
* $bitOr('$myInt', '$myLong');
* // returns
* { $bitOr: ['$myInt', '$myLong'] }
*/
const $bitOr = ptafaa('$bitOr');

/**
* Returns the result of a bitwise xor operation on an array of int and long
* values.
* @category Operators
* @function
* @param {...ObjectExpression} expressions int or long values.
* @see {@link https://www.mongodb.com/docs/manual/reference/operator/aggregation/bitXor/|MongoDB reference}
* for $bitXor
* @example
* $bitXor('$myInt', '$myLong');
* // returns
* { $bitXor: ['$myInt', '$myLong'] }
*/
const $bitXor = ptafaa('$bitXor');

type BsonSizeOperator = {
$bsonSize: ObjectExpression | null,
};
Expand Down Expand Up @@ -2765,6 +2826,19 @@ type NeOperator = {
*/
const $ne = at('$ne');

// TODO
const $nin = taf('$nin');

// TODO
// * @category Safe Operators
const $ninSafe = (...args: any[]) => {
if (args.length === 2) {
const [val, arr] = args;
return $in(val, $ifNull(arr, []));
}
return $in(...args);
};

type NotOperator = {
$not: Expression,
};
Expand Down Expand Up @@ -3057,6 +3131,8 @@ type SizeOperator = {
*/
const $size = se('$size');

const $slice = pta('$slice');

type SplitOperator = {
$split: [StringExpression, StringExpression],
};
Expand Down Expand Up @@ -3203,6 +3279,10 @@ const $subtract = at('$subtract');
*/
const $subtractSafe = safeNumberArgs($subtract);

const $substr = pta('$substr');
const $substrBytes = pta('$substrBytes');
const $substrCP = pta('$substrCP');

type SumOperator = {
$sum: Expression,
};
Expand Down Expand Up @@ -3636,6 +3716,14 @@ export = {
$binarySize,
binarySize: $binarySize,
branch,
$bitAnd,
bitAnd: $bitAnd,
$bitNot,
bitNot: $bitNot,
$bitOr,
bitOr: $bitOr,
$bitXor,
bitXor: $bitXor,
$bsonSize,
bsonSize: $bsonSize,
case: branch,
Expand Down Expand Up @@ -3769,6 +3857,8 @@ export = {
multiplySafe: $multiplySafe,
$ne,
ne: $ne,
$nin,
nin: $nin,
$not,
not: $not,
$or,
Expand Down Expand Up @@ -3822,12 +3912,20 @@ export = {
split: $split,
$strcasecmp,
strcasecmp: $strcasecmp,
$substr,
substr: $substr,
$substrBytes,
substrBytes: $substrBytes,
$substrCP,
substrCP: $substrCP,
$subtract,
subtract: $subtract,
$subtractSafe,
subtractSafe: $subtractSafe,
$size,
size: $size,
$slice,
slice: $slice,
$sin,
sin: $sin,
$sinh,
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,6 @@
],
"volta": {
"node": "20.11.0",
"yarn": "4.1.1"
"yarn": "1.22.22"
}
}
46 changes: 46 additions & 0 deletions pipeline.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -653,6 +653,19 @@ describe('aggregation', () => {
expect($.binarySize('$value')).toEqual({ $binarySize: '$value' });
});
});
describe('$bitAnd', () => {
it('exports expected vars', () => {
expect($.bitAnd).toBeDefined();
expect($.$bitAnd).toBeDefined();
expect($.bitAnd).toStrictEqual($.$bitAnd);
});
it('returns expected result', () => {
expect($.bitAnd('$a', '$b')).toEqual({ $bitAnd: ['$a', '$b'] });
});
it('supports array input', () => {
expect($.bitAnd(['$a', '$b'])).toEqual({ $bitAnd: ['$a', '$b'] });
});
});
describe('$bsonSize', () => {
it('exports expected vars', () => {
expect($.bsonSize).toBeDefined();
Expand Down Expand Up @@ -1325,6 +1338,18 @@ describe('aggregation', () => {
expect($.ne('$value', 3)).toEqual({ $ne: ['$value', 3] });
});
});
describe('$nin', () => {
it('exports expected vars', () => {
expect($.nin).toBeDefined();
expect($.$nin).toBeDefined();
expect($.nin).toStrictEqual($.$nin);
});
it('returns expected result', () => {
expect($.nin('$myArray')).toEqual({ $nin: '$myArray' });
expect($.nin([1, 3, 5])).toEqual({ $nin: [1, 3, 5] });
expect($.nin('$val', '$myArray')).toEqual({ $nin: ['$val', '$myArray'] });
});
});
describe('$not', () => {
it('exports expected vars', () => {
expect($.not).toBeDefined();
Expand Down Expand Up @@ -1507,6 +1532,16 @@ describe('aggregation', () => {
expect($.size('$myArray')).toEqual({ $size: '$myArray' });
});
});
describe('$slice', () => {
it('exports expected vars', () => {
expect($.slice).toBeDefined();
expect($.$slice).toBeDefined();
expect($.slice).toStrictEqual($.$slice);
});
it('returns expected result', () => {
expect($.slice('$myArray', 2)).toEqual({ $slice: ['$myArray', 2] });
});
});
describe('$split', () => {
it('exports expected vars', () => {
expect($.split).toBeDefined();
Expand Down Expand Up @@ -1589,6 +1624,17 @@ describe('aggregation', () => {
});
});
});
describe('$substr', () => {
it('exports expected vars', () => {
expect($.substr).toBeDefined();
expect($.$substr).toBeDefined();
expect($.substr).toStrictEqual($.$substr);
});
it('returns expected result', () => {
expect($.substr('$value', 1)).toEqual({ $substr: ['$value', 1] });
expect($.substr('$value', 1, 2)).toEqual({ $substr: ['$value', 1, 2] });
});
});
describe('$sum', () => {
it('exports expected vars', () => {
expect($.sum).toBeDefined();
Expand Down
Loading

0 comments on commit 595f95d

Please sign in to comment.