Best JavaScript code snippet using fast-check-monorepo
rules.test.ts
Source:rules.test.ts
1import Big from "big.js";2import moment from "moment";3import { Rules } from "./rules";4import { RawRecord } from "./transaction";5test("Transforms a RuleDesc such that additionalCondition can be eval'd", () => {6 const ruleDesc: Rules.RuleDesc = {7 regex: ".*",8 category: "coffee/hip",9 additionalCheck: "isDebit({ min: 4.0 })",10 };11 const rule = Rules.toRule(ruleDesc);12 expect(rule.additionalCheck).toBeInstanceOf(Function);13 expect(rule.additionalCheck!(withAmount(-3))).toEqual(false);14 expect(rule.additionalCheck!(withAmount(-5))).toEqual(true);15});16test("or() can be used in rule additionalChecks (not `||`)", () => {17 const ruleDesc: Rules.RuleDesc = {18 regex: ".*",19 category: "what",20 additionalCheck: "isDebit({ eq: 4.0 }).or(isCredit({ eq: 4.0 }))",21 };22 const rule = Rules.toRule(ruleDesc);23 expect(rule.additionalCheck).toBeInstanceOf(Function);24 expect(rule.additionalCheck!(withAmount(-4))).toEqual(true);25 expect(rule.additionalCheck!(withAmount(+4))).toEqual(true);26 expect(rule.additionalCheck!(withAmount(+5))).toEqual(false);27 expect(rule.additionalCheck!(withAmount(-5))).toEqual(false);28});29test("regexp with no slash is used with no flags", () => {30 const rule = Rules.toRule(withRegexpString("^foo"));31 expect(rule.regex.test("foo bar")).toEqual(true);32 expect(rule.regex.test("bar foo")).toEqual(false);33 expect(rule.regex.test("Foo bar")).toEqual(false);34});35test("regexp slashes and no flags is fine", () => {36 const rule = Rules.toRule(withRegexpString("/^foo/"));37 expect(rule.regex.test("foo bar")).toEqual(true);38 expect(rule.regex.test("bar foo")).toEqual(false);39 expect(rule.regex.test("Foo bar")).toEqual(false);40});41test("regexp slashes and flags is taking flags into account", () => {42 const rule = Rules.toRule(withRegexpString("/^foo/i"));43 expect(rule.regex.test("foo bar")).toEqual(true);44 expect(rule.regex.test("bar foo")).toEqual(false);45 expect(rule.regex.test("Foo bar")).toEqual(true);46 expect(rule.regex.test("fOO Bar")).toEqual(true);47});48test("regexp slashes needs at least both slashes or throws exception", () => {49 expect(() => Rules.toRule(withRegexpString("/^foo"))).toThrow(50 "Invalid regular expression"51 );52});53// Not actually super keen to make this work54test.skip("regex only last slash throws exception", () => {55 expect(() => Rules.toRule(withRegexpString("^foo/"))).toThrow(56 "Invalid regular expression"57 );58 expect(() => Rules.toRule(withRegexpString("^foo/i"))).toThrow(59 "Invalid regular expression"60 );61});62test("regexp slashes and invalid flag throws exception", () => {63 expect(() => Rules.toRule(withRegexpString("/^foo/z"))).toThrow(64 /invalid flags/i65 );66});67const withRegexpString = (r: string): Rules.RuleDesc => ({68 regex: r,69 category: "lol",70});71const withAmount = (n: number): RawRecord => {72 return {73 id: "ignore me",74 account: { id: "1", name: "test account" },75 date: moment(),76 desc: "test record",77 amount: new Big(n),78 };...
rules.ts
Source:rules.ts
1import * as yaml from "js-yaml";2import { evalAsFunction } from "../util/eval";3import { Logger } from "../util/log";4import { Category } from "./category";5import { RawRecord } from "./transaction";6export namespace Rules {7 type AdditionalCheck = (transaction: RawRecord) => boolean;8 type AdditionalCheckTS = string;9 /* The rule, pretty much as described in the yaml file -- could even have comments, line numbers, the lot */10 export interface RuleDesc {11 readonly merchant?: string;12 readonly category: string;13 readonly regex: string;14 readonly additionalCheck?: AdditionalCheckTS;15 }16 /* The rule, as used at runtime */17 export interface Rule {18 readonly merchant?: string;19 readonly category: Category;20 readonly regex: RegExp;21 readonly additionalCheck?: AdditionalCheck;22 }23 export const extractCategories = (rules: RuleDesc[]) => {24 const allCats = rules.map((r) => r.category);25 // remove dupes26 return Array.from(new Set(allCats)).sort();27 };28 // For transactions and dateRange, we're doing conversions in worker/transfer,29 // to workaround the types of Big.js and moment.js that aren't "transferrable" between webworkers.30 // Here, we want to eval/type a Function from a string, which is incompatible but we also don't want31 // to eval it every time we send work to worker?32 export const toRule: (r: RuleDesc) => Rule = (r: RuleDesc) => {33 return {34 regex: parseRegex(r.regex),35 category: r.category,36 merchant: r.merchant,37 additionalCheck: r.additionalCheck38 ? evalRule(r.additionalCheck)39 : undefined,40 };41 };42 function parseRegex(regexStr: string): RegExp {43 if (regexStr[0] === "/") {44 const lastIndexOf = regexStr.lastIndexOf("/");45 if (lastIndexOf < 1) {46 throw new Error("Invalid regular expression");47 }48 const exp = regexStr.slice(1, lastIndexOf);49 const flags = regexStr.slice(lastIndexOf + 1);50 return new RegExp(exp, flags);51 } else {52 return new RegExp(regexStr);53 }54 }55 function evalRule(additionalCheck: AdditionalCheckTS): AdditionalCheck {56 return evalAsFunction<RawRecord, boolean>(additionalCheck);57 }58 export class RulesLoader {59 constructor(readonly log: Logger) {60 this.log = log;61 }62 loadYaml(yamlContent: string): RuleDesc[] {63 const doc = yaml.load(yamlContent);64 if (!Array.isArray(doc)) {65 throw new Error("Rules yaml should be an array");66 } else {67 return Array.from(doc).map((yamlElement) => {68 return {69 merchant: yamlElement["merchant"],70 category: yamlElement["category"],71 regex: yamlElement["regex"],72 additionalCheck: yamlElement["additionalCheck"],73 };74 });75 }76 }77 }...
filters.ts
Source:filters.ts
...21 getValue(u.value) > 0 || getValue(u.value) === -100;22 }23 return (item: AnnoItem) =>24 filterValue === "all" ||25 item.upgrades.some((u) => u.key === filterValue && additionalCheck(u));26}27export function byRarity(filterValue: string) {28 return (item: AnnoItem) =>29 filterValue === "all" || item.rarityLabel === filterValue;30}31export function byFavourite(filterValue: boolean) {32 return (item: AnnoItem) => filterValue === false || item.favourite;33}34function getValue(value: number | { Value: number }) {35 return typeof value === "number" ? value : value.Value;...
Using AI Code Generation
1const additionalCheck = require("fast-check-monorepo").additionalCheck;2const fc = require("fast-check");3const isEven = (n) => n % 2 === 0;4const isOdd = (n) => n % 2 !== 0;5describe("additionalCheck", () => {6 test("isEven", () =>7 additionalCheck(8 fc.integer(),9 (n) => isEven(n) || isOdd(n),10 ));11});12- [additionalCheck](#additionalcheck)13 - [Parameters](#parameters)14 - [Examples](#examples)15- [additionalCheckAsync](#additionalcheckasync)16 - [Parameters](#parameters-1)17 - [Examples](#examples-1)18- [additionalCheckProperty](#additionalcheckproperty)19 - [Parameters](#parameters-2)20 - [Examples](#examples-2)21- [additionalCheckPropertyAsync](#additionalcheckpropertyasync)22 - [Parameters](#parameters-3)23 - [Examples](#examples-3)24- [additionalCheckPropertyWithConfig](#additionalcheckpropertywithconfig)25 - [Parameters](#parameters-4)26 - [Examples](#examples-4)27- [additionalCheckPropertyWithConfigAsync](#additionalcheckpropertywithconfigasync)28 - [Parameters](#parameters-5)29 - [Examples](#examples-5)30- [additionalCheckPropertyWithConfigAndSeed](#additionalcheckpropertywithconfigandseed)31 - [Parameters](#parameters-6)32 - [Examples](#examples-6)33- [additionalCheckPropertyWithConfigAndSeedAsync](#additionalcheckpropertywithconfigandseedasync)34 - [Parameters](#parameters-7)35 - [Examples](#examples-7)36- [additionalCheckPropertyWithSeed](#additionalcheckpropertywithseed)37 - [Parameters](#parameters-8)38 - [Examples](#examples-8)39- [additionalCheckPropertyWithSeedAsync](#additionalcheckpropertywithseedasync)40 - [Parameters](#parameters-9)
Using AI Code Generation
1const fc = require('fast-check');2const additionalCheck = require('fast-check-monorepo/additionalCheck');3fc.assert(4 fc.property(fc.integer(), fc.integer(), (a, b) => {5 return a + b >= a && a + b >= b;6 })7);
Using AI Code Generation
1const fc = require("fast-check");2fc.assert(3 fc.property(fc.integer(), fc.integer(), (a, b) => {4 return additionalCheck(a, b);5 })6);7 ✓ should hold (4ms)
Using AI Code Generation
1const additionalCheck = require('fast-check-monorepo/additionalCheck');2const additionalCheck = require('fast-check-monorepo');3const additionalCheck = require('fast-check-monorepo');4const additionalCheck = require('fast-check-monorepo');5const additionalCheck = require('fast-check-monorepo');6const additionalCheck = require('fast-check-monorepo');7const additionalCheck = require('fast-check-monorepo');
Learn to execute automation testing from scratch with LambdaTest Learning Hub. Right from setting up the prerequisites to run your first automation test, to following best practices and diving deeper into advanced test scenarios. LambdaTest Learning Hubs compile a list of step-by-step guides to help you be proficient with different test automation frameworks i.e. Selenium, Cypress, TestNG etc.
You could also refer to video tutorials over LambdaTest YouTube channel to get step by step demonstration from industry experts.
Get 100 minutes of automation test minutes FREE!!