n8n/packages/workflow/test/ExpressionExtensions/NumberExtensions.test.ts

60 lines
2.1 KiB
TypeScript
Raw Normal View History

feat: Expression extension framework (#4372) * :zap: Introduce a framework for expression extension * :bulb: Add some inline comments * :zap: Introduce hash alias for encrypt * :zap: Introduce a manual granular level approach to shadowing/overrideing extensions * :fire: Cleanup comments * :zap: Introduce a basic method of extension for native functions * :zap: Add length to StringExtension * :zap: Add number type to extension return types * :zap: Temporarily introduce DateTime with extension * :zap: Cleanup comments * :zap: Organize imports * :recycle: Fix up some typings * :zap: Fix typings * :recycle: Remove unnecessary resolve of expression * :zap: Extensions Improvement * :recycle: Refactor EXPRESSION_EXTENSION_METHODS * :recycle: Refactor EXPRESSION_EXTENSION_METHODS * :recycle: Update extraArgs types * :recycle: Fix tests * :recycle: Fix bind type issue * :recycle: Fixing duration type issue * :recycle: Refactor to allow overrides on native methods * :recycle: Temporarily remove Date Extensions to pass tests * feat(dt-functions): introduce date expression extensions (#4045) * :tada: Add Date Extensions into the mix * :sparkles: Introduce additional date extension methods * :white_check_mark: Add Date Expression Extension tests * :wrench: Add ability to debug tests * :recycle: Refactor extension for native types * :fire: Move sayHi method to String Extension class * :recycle: Update scope when binding member methods * :white_check_mark: Add String Extension tests * feat(dt-functions): introduce array expression extensions (#4044) * :sparkles: Introduce Array Extensions * :white_check_mark: Add Array Expression tests * feat(dt-functions): introduce number expression extensions (#4046) * :tada: Introduce Number Extensions * :zap: Support more shared extensions * :zap: Improve handling of name collision * :white_check_mark: Update tests * Fixed up tests * :fire: Remove remove markdown * :recylce: Replace remove-markdown dependencies with implementation * :recycle: Replace remove-markdown dependencies with implementation * :white_check_mark: Update tests * :recycle: Fix scoping and cleanup * :recycle: Update comments and errors * :recycle: Fix linting errors * :heavy_minus_sign: Remove unused dependencies * fix: expression extension not working with multiple extensions * refactor: change extension transform to be more efficient * test: update most test to work with new extend function * fix: update and fix type error in config * refactor: replace babel with recast * feat: add hashing functions to string extension * fix: removed export * test: add extension parser and transform tests * fix: vite tests breaking * refactor: remove commented out code * fix: parse dates passed from $json in extend function * refactor: review feedback changes for date extensions * refactor: review feedback changes for number extensions * fix: date extension beginningOf test * fix: broken build from merge * fix: another merge issue * refactor: address review feedback (remove ignores) * feat: new extension functions and tests * feat: non-dot notation functions * test: most of the other tests * fix: toSentenceCase for node versions below 16.6 * feat: add $if and $not expression extensions * Fix test to work on every timezone * lint: fix remaining lint issues Co-authored-by: Csaba Tuncsik <csaba@n8n.io> Co-authored-by: Omar Ajoue <krynble@gmail.com>
2023-01-10 05:06:12 -08:00
/**
* @jest-environment jsdom
*/
import { numberExtensions } from '@/Extensions/NumberExtensions';
import { evaluate } from './Helpers';
describe('Data Transformation Functions', () => {
describe('Number Data Transformation Functions', () => {
test('.format() should work correctly on a number', () => {
expect(evaluate('={{ Number(100).format() }}')).toEqual(
numberExtensions.functions.format(100, []),
);
});
test('.ceil() should work on a number', () => {
expect(evaluate('={{ (1.2).ceil() }}')).toEqual(2);
expect(evaluate('={{ (1.9).ceil() }}')).toEqual(2);
expect(evaluate('={{ (1.0).ceil() }}')).toEqual(1);
expect(evaluate('={{ (NaN).ceil() }}')).toBeNaN();
});
test('.floor() should work on a number', () => {
expect(evaluate('={{ (1.2).floor() }}')).toEqual(1);
expect(evaluate('={{ (1.9).floor() }}')).toEqual(1);
expect(evaluate('={{ (1.0).floor() }}')).toEqual(1);
expect(evaluate('={{ (NaN).floor() }}')).toBeNaN();
});
test('.round() should work on a number', () => {
expect(evaluate('={{ (1.3333333).round(3) }}')).toEqual(1.333);
expect(evaluate('={{ (1.3333333).round(0) }}')).toEqual(1);
expect(evaluate('={{ (1.5001).round(0) }}')).toEqual(2);
expect(evaluate('={{ (NaN).round(3) }}')).toBeNaN();
});
test('.isOdd() should work on a number', () => {
expect(evaluate('={{ (9).isOdd() }}')).toEqual(true);
expect(evaluate('={{ (8).isOdd() }}')).toEqual(false);
expect(evaluate('={{ (0).isOdd() }}')).toEqual(false);
expect(evaluate('={{ (NaN).isOdd() }}')).toEqual(false);
});
test('.isEven() should work on a number', () => {
expect(evaluate('={{ (9).isEven() }}')).toEqual(false);
expect(evaluate('={{ (8).isEven() }}')).toEqual(true);
expect(evaluate('={{ (0).isEven() }}')).toEqual(true);
expect(evaluate('={{ (NaN).isEven() }}')).toEqual(false);
});
});
describe('Multiple expressions', () => {
test('Basic multiple expressions', () => {
feat(editor): Completions for extensions in expression editor (#5130) * :fire: Remove test extensions * :construction: Add test description * :blue_book: Expand types * :zap: Export extensions * :zap: Export collection * :zap: Mark all proxies * :pencil2: Rename for clarity * :zap: Export from barrel * :sparkles: Create datatype completions * :zap: Mount datatype completions * :test_tube: Adjust tests * :zap: Add `path` prop * :fire: Remove `()` from completion labels * :zap: Filter out completions for pseudo-proxies * :bug: Fix method error * :zap: Add metrics * :pencil2: Improve naming * :sparkles: Start completion on empty resolvable * :sparkles: Implement completion previews * :zap: Break out completion manager * :zap: Implement in expression editor modal * :pencil2: Improve naming * :zap: Filter out irrelevant completions * :sparkles: Add preview hint * :pencil2: Improve comments * :art: Style preview hint * :zap: Expand `hasNoParams` * :zap: Add spacing for readability * :zap: Add error codes * :pencil2: Add comment * :bug: Fix Esc behavior * :zap: Parse Unicode * :zap: Throw on invalid `DateTime` * :zap: Fix second root completion detection * :zap: Switch message at completable prefix position * :bug: Fix function names for non-dev build * :bug: Fix `json` handling * :fire: Comment out previews * :recycle: Apply feedback * :fire: Remove extensions * :truck: Rename extensions * :zap: Adjust some implementations * :fire: Remove dummy extensions * :bug: Fix object regex * :recycle: Apply feedback * :pencil2: Fix typos * :pencil2: Add `fn is not a function` message * :fire: Remove check * :sparkles: Add `isNotEmpty` for objects * :truck: Rename `global` to `alpha` * :fire: Remove `encrypt` * :zap: Restore `is not a function` error * :zap: Support `week` on `extract()` * :test_tube: Fix tests * :zap: Add validation to some string extensions * :zap: Validate number arrays in some extensions * :test_tube: Fix tests * :pencil2: Improve error message * :rewind: Revert extensions framework changes * :broom: Previews cleanup * :zap: Condense blank completions * :zap: Refactor dollar completions * :zap: Refactor non-dollar completions * :zap: Refactor Luxon completions * :zap: Refactor datatype completions * :zap: Use `DATETIMEUNIT_MAP` * :pencil2: Update test description * :rewind: Revert "Use `DATETIMEUNIT_MAP`" This reverts commit 472a77df5cd789905d162f3c3db02ac767b89b4e. * :test_tube: Add tests * :recycle: Restore generic extensions * :fire: Remove logs * :test_tube: Expand tests * :sparkles: Add `Math` completions * :pencil2: List breaking change * :zap: Add doc tooltips * :bug: Fix node selector regex * :bug: Fix `context` resolution * :bug: Allow dollar completions in args * :zap: Make numeric array methods context-dependent * :pencil: Adjust docs * :bug: Fix selector ref * :zap: Surface error for valid URL * :bug: Disallow whitespace in `isEmail` check * :test_tube: Fix test for `isUrl` * :zap: Add comma validator in `toFloat` * :zap: Add validation to `$jmespath()` * :rewind: Revert valid URL error * :zap: Adjust `$jmespath()` validation * :test_tube: Adjust `isUrl` test * :zap: Remove `{}` and `[]` from compact * :pencil2: Update docs * :truck: Rename `stripTags` to `removeTags` * :zap: Do not inject whitespace inside resolvable * :zap: Make completions aware of `()` * :pencil2: Add note * :zap: Update sorting * :zap: Hide active node name from node selector * :fire: Remove `length()` and its aliases * :zap: Validate non-zero for `chunk` * :pencil2: Reword all error messages * :bug: Fix `$now` and `$today` * :zap: Simplify with `stripExcessParens` * :zap: Fold luxon into datatype * :test_tube: Clean up tests * :fire: Remove tests for removed methods * :shirt: Fix type * :arrow_up: Upgrade lang pack * :rewind: Undo change to `vitest` command * :fire: Remove unused method * :zap: Separate `return` line * :pencil2: Improve description * :test_tube: Expand tests for initial-only completions * :test_tube: Add bracket-aware completions * :zap: Make check for `all()` stricter * :pencil2: Adjust explanatory comments * :fire: Remove unneded copy * :fire: Remove outdated comment * :zap: Make naming consistent * :pencil2: Update comments * :zap: Improve URL scheme check * :pencil2: Add comment * :truck: Move extension * :pencil2: Update `BREAKING-CHANGES.md` * :pencil2: Update upcoming version * :pencil2: Fix grammar * :pencil2: Shorten message * :bug: Fix `Esc` behavior * :bug: Fix `isNumeric` * :sparkles: Support native methods * :test_tube: Skip Pinia tests * :pencil2: Shorten description * :fire: Remove outdated comment * :test_tube: Unskip Pinia tests * :pencil2: Add comments * :test_tube: Expand tests to natives * :pencil2: Add clarifying comments * :zap: Use `setTimeout` to make telemetry non-blocking * :bug: Account for no active node in cred modal * :sparkles: Resolve without workflow * :fire: Remove `Esc` handling on NDV * :zap: Use `isDateTime` * :truck: Move `unique` to next phase This array extension takes optional args. * :zap: Merge export * :test_tube: Fix tests * :rewind: Restore check * :pencil2: Make breaking change description more accurate * :test_tube: Fix e2e tests
2023-02-02 03:35:38 -08:00
expect(evaluate('={{ "test abc".toSnakeCase() }} you have ${{ (100).format() }}.')).toEqual(
'test_abc you have $100.',
feat: Expression extension framework (#4372) * :zap: Introduce a framework for expression extension * :bulb: Add some inline comments * :zap: Introduce hash alias for encrypt * :zap: Introduce a manual granular level approach to shadowing/overrideing extensions * :fire: Cleanup comments * :zap: Introduce a basic method of extension for native functions * :zap: Add length to StringExtension * :zap: Add number type to extension return types * :zap: Temporarily introduce DateTime with extension * :zap: Cleanup comments * :zap: Organize imports * :recycle: Fix up some typings * :zap: Fix typings * :recycle: Remove unnecessary resolve of expression * :zap: Extensions Improvement * :recycle: Refactor EXPRESSION_EXTENSION_METHODS * :recycle: Refactor EXPRESSION_EXTENSION_METHODS * :recycle: Update extraArgs types * :recycle: Fix tests * :recycle: Fix bind type issue * :recycle: Fixing duration type issue * :recycle: Refactor to allow overrides on native methods * :recycle: Temporarily remove Date Extensions to pass tests * feat(dt-functions): introduce date expression extensions (#4045) * :tada: Add Date Extensions into the mix * :sparkles: Introduce additional date extension methods * :white_check_mark: Add Date Expression Extension tests * :wrench: Add ability to debug tests * :recycle: Refactor extension for native types * :fire: Move sayHi method to String Extension class * :recycle: Update scope when binding member methods * :white_check_mark: Add String Extension tests * feat(dt-functions): introduce array expression extensions (#4044) * :sparkles: Introduce Array Extensions * :white_check_mark: Add Array Expression tests * feat(dt-functions): introduce number expression extensions (#4046) * :tada: Introduce Number Extensions * :zap: Support more shared extensions * :zap: Improve handling of name collision * :white_check_mark: Update tests * Fixed up tests * :fire: Remove remove markdown * :recylce: Replace remove-markdown dependencies with implementation * :recycle: Replace remove-markdown dependencies with implementation * :white_check_mark: Update tests * :recycle: Fix scoping and cleanup * :recycle: Update comments and errors * :recycle: Fix linting errors * :heavy_minus_sign: Remove unused dependencies * fix: expression extension not working with multiple extensions * refactor: change extension transform to be more efficient * test: update most test to work with new extend function * fix: update and fix type error in config * refactor: replace babel with recast * feat: add hashing functions to string extension * fix: removed export * test: add extension parser and transform tests * fix: vite tests breaking * refactor: remove commented out code * fix: parse dates passed from $json in extend function * refactor: review feedback changes for date extensions * refactor: review feedback changes for number extensions * fix: date extension beginningOf test * fix: broken build from merge * fix: another merge issue * refactor: address review feedback (remove ignores) * feat: new extension functions and tests * feat: non-dot notation functions * test: most of the other tests * fix: toSentenceCase for node versions below 16.6 * feat: add $if and $not expression extensions * Fix test to work on every timezone * lint: fix remaining lint issues Co-authored-by: Csaba Tuncsik <csaba@n8n.io> Co-authored-by: Omar Ajoue <krynble@gmail.com>
2023-01-10 05:06:12 -08:00
);
});
});
});