Skip to content
Draft
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
4 changes: 4 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,10 @@
"./operations": {
"require": "./cjs/operations.js",
"default": "./mjs/operations.js"
},
"./codec": {
"require": "./cjs/codec/index.js",
"default": "./mjs/codec/index.js"
}
},
"files": [
Expand Down
36 changes: 36 additions & 0 deletions src/__tests__/codec.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
// Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
// SPDX-License-Identifier: Apache-2.0
import { test, expect, describe } from 'vitest';
import { encode, decode } from '../codec';
import {
tokenGroupSimple,
tokenGroupWithArrays,
tokenGroupWithArraysAndDates,
tokensSimple,
tokensWithArrays,
tokensWithArraysAndDates,
} from './property-filter-fixtures';

describe('codec', () => {
test.each([
tokenGroupSimple,
tokenGroupWithArrays,
tokenGroupWithArraysAndDates,
tokensSimple,
tokensWithArrays,
tokensWithArraysAndDates,
])('encode/decode property filters', propertyFilter => {
const encoded = encode(propertyFilter);
const decoded = decode(encoded);
expect(decoded).toStrictEqual(propertyFilter);
});

test('decode should return null if the input string is invalid json and defaultResult is not provided', () => {
expect(decode('invalid json')).toBeNull();
});

test('decode should return the defaultResult if the input string is invalid json and defaultResult is provided', () => {
const defaultResult = { operation: 'and', tokens: [], tokenGroups: [] };
expect(decode('invalid json', defaultResult)).toStrictEqual(defaultResult);
});
});
191 changes: 191 additions & 0 deletions src/__tests__/property-filter-fixtures.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,191 @@
// Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
// SPDX-License-Identifier: Apache-2.0

export const tokenGroupSimple = {
tokens: [],
operation: 'and',
tokenGroups: [
{
propertyKey: 'instanceid',
operator: '=',
value: 'i-2dc5ce28a0328391',
},
],
};

export const tokenGroupWithArrays = {
tokens: [],
operation: 'and',
tokenGroups: [
{
propertyKey: 'instanceid',
operator: '=',
value: 'i-2dc5ce28a0328391',
},
{
propertyKey: 'state',
operator: '=',
value: ['STOPPED', 'STOPPING', 'PENDING'],
},
{
propertyKey: 'stopped',
operator: '=',
value: true,
},
{
propertyKey: 'instancetype',
operator: '=',
value: 't2.medium',
},
],
};

export const tokenGroupWithArraysAndDates = {
tokens: [],
operation: 'and',
tokenGroups: [
{
propertyKey: 'instanceid',
operator: '=',
value: 'i-2dc5ce28a0328391',
},
{
propertyKey: 'state',
operator: '=',
value: ['STOPPED', 'STOPPING', 'PENDING'],
},
{
propertyKey: 'stopped',
operator: '=',
value: true,
},
{
propertyKey: 'instancetype',
operator: '=',
value: 't2.medium',
},
{
propertyKey: 'averagelatency',
operator: '<=',
value: '123',
},
{
propertyKey: 'availablestorage',
operator: '=',
value: '6.86',
},
{
propertyKey: 'owner',
operator: '=',
value: ['admin512', 'admin0', 'admin6621'],
},
{
propertyKey: 'privateipaddress',
operator: ':',
value: '116.198.231.86',
},
{
propertyKey: 'publicdns',
operator: '=',
value: 'ec2-23-50-59-84.us-west-1.compute.amazonaws.com',
},
{
propertyKey: 'publicdns',
operator: ':',
value: 'ec2-11-56-64-85.eu-west-2.compute.amazonaws.com',
},
{
propertyKey: 'ipv4publicip',
operator: ':',
value: '90.77.83.18',
},
{
propertyKey: 'securitygroup',
operator: '!=',
value: 'launch-wizard-66',
},
{
propertyKey: 'releasedate',
operator: '>=',
value: '2024-12-21',
},
],
};

export const tokensSimple = {
tokens: [
{
propertyKey: 'instanceid',
operator: '=',
value: 'i-2dc5ce28a0328391',
},
],
operation: 'and',
};

export const tokensWithArrays = {
tokens: [
{
propertyKey: 'instanceid',
operator: '=',
value: 'i-2dc5ce28a0328391',
},
{
propertyKey: 'state',
operator: '!=',
value: ['STOPPING', 'PENDING'],
},
{
propertyKey: 'stopped',
operator: '=',
value: false,
},
{
propertyKey: 'instancetype',
operator: ':',
value: 't3.nano',
},
],
operation: 'and',
};

export const tokensWithArraysAndDates = {
tokens: [
{
propertyKey: 'instanceid',
operator: '=',
value: 'i-2dc5ce28a0328391',
},
{
propertyKey: 'state',
operator: '!=',
value: ['STOPPING', 'PENDING'],
},
{
propertyKey: 'stopped',
operator: '=',
value: false,
},
{
propertyKey: 'instancetype',
operator: ':',
value: 't3.nano',
},
{
propertyKey: 'releasedate',
operator: '<=',
value: '2024-12-20',
},
{
propertyKey: 'launchdate',
operator: '>',
value: '2024-12-01',
},
{
propertyKey: 'tags',
operator: '!:',
value: ['AAA', 'AB'],
},
],
operation: 'and',
};
16 changes: 16 additions & 0 deletions src/codec/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
// Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
// SPDX-License-Identifier: Apache-2.0

export function encode<T>(queryObject: T): string {
return encodeURIComponent(JSON.stringify(queryObject));
}

export function decode<T>(queryString: string, defaultResult: T): T;
export function decode<T>(queryString: string, defaultResult?: undefined): T | null;
export function decode<T>(queryString: string, defaultResult?: T): T | null {
try {
return JSON.parse(decodeURIComponent(queryString));
} catch (error) {
return defaultResult ?? null;
}
}