forked from n8n-io/n8n
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(dt-functions): introduce number expression extensions (n8n-io#4046)
* 🎉 Introduce Number Extensions * ⚡ Support more shared extensions * ⚡ Improve handling of name collision * ✅ Update tests
- Loading branch information
Showing
8 changed files
with
161 additions
and
52 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,61 @@ | ||
/* eslint-disable @typescript-eslint/unbound-method */ | ||
/* eslint-disable @typescript-eslint/explicit-member-accessibility */ | ||
// eslint-disable-next-line import/no-cycle | ||
import { BaseExtension, ExtensionMethodHandler } from './Extensions'; | ||
|
||
export class NumberExtensions extends BaseExtension<number> { | ||
methodMapping = new Map<string, ExtensionMethodHandler<number>>(); | ||
|
||
constructor() { | ||
super(); | ||
this.initializeMethodMap(); | ||
} | ||
|
||
bind(mainArg: number, extraArgs?: number[] | string[] | boolean[] | undefined) { | ||
return Array.from(this.methodMapping).reduce((p, c) => { | ||
const [key, method] = c; | ||
Object.assign(p, { | ||
[key]: () => { | ||
return method.call(this, mainArg, extraArgs); | ||
}, | ||
}); | ||
return p; | ||
}, {} as object); | ||
} | ||
|
||
private initializeMethodMap(): void { | ||
this.methodMapping = new Map< | ||
string, | ||
( | ||
value: number, | ||
extraArgs?: number | number[] | string[] | boolean[] | undefined, | ||
) => boolean | string | Date | number | ||
>([]); | ||
} | ||
|
||
format(value: number, extraArgs?: any): string { | ||
const [locales = 'en-US', config] = extraArgs as [ | ||
string | string[], | ||
{ compactDisplay: string; notation: string; style: string }, | ||
]; | ||
|
||
return new Intl.NumberFormat(locales, { | ||
...config, | ||
notation: 'compact', | ||
compactDisplay: 'short', | ||
style: 'decimal', | ||
}).format(value); | ||
} | ||
|
||
isBlank(value: number): boolean { | ||
return value == null || typeof value !== 'number'; | ||
} | ||
|
||
isPresent(value: number): boolean { | ||
return !this.isBlank(value); | ||
} | ||
|
||
random(value: number): number { | ||
return Math.floor(Math.random() * value); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters