Best JavaScript code snippet using fast-check-monorepo
pull.ts
Source:pull.ts
1import { MessageAttachment, MessageEmbed } from 'discord.js';2import { HeroForm } from '@cquest/entities';3import { heroes } from '@cquest/data-provider';4import {5 random, makeInRange, makePullImage, pickGrade, formatPullChunks, chancesRoll, stringTuple6} from '../util/functions';7import BaseCommand from './abstract/BaseCommand';8import {9 CommandArguments, CommandCategory, CommandPayload, CommandResult, CommandResultCode, RollChances10} from '../common-types';11const reZeroPool: RollChances = {12 CHA_WI_LIMITED_RZ_4_05: 1,13 CHA_PA_LIMITED_RZ_4_03: 1,14 CHA_PR_LIMITED_RZ_4_07: 3,15 CHA_WA_LIMITED_RZ_4_02: 3,16 CHA_WI_LIMITED_RZ_4_04: 3,17 CHA_WI_LIMITED_RZ_4_06: 3,18};19const gg1Pool: RollChances = {20 CHA_WI_LIMITED_GG_4_1: 1,21 CHA_WA_LIMITED_GG_4_2: 1,22 CHA_HU_LIMITED_GG_4_1: 3,23 CHA_PR_LIMITED_GG_4_1: 3,24 CHA_WA_LIMITED_GG_4_1: 3,25 CHA_PA_LIMITED_GG_4_1: 3,26 CHA_AR_LIMITED_GG_4_1: 3,27};28const gg2Pool: RollChances = {29 CHA_WI_LIMITED_GG3_4_DIZZY: 1,30 CHA_WA_LIMITED_GG_4_4: 1,31 CHA_WA_LIMITED_GG3_4_BAIKEN: 3,32 CHA_WA_LIMITED_GG_4_3: 3,33 CHA_PA_LIMITED_GG_4_2: 3,34 CHA_WI_LIMITED_GG_4_2: 3,35};36const gsPool: RollChances = {37 CHA_PA_LIMITED_GS_4_02: 10,38 CHA_WA_LIMITED_GS_4_01: 10,39 CHA_AR_LIMITED_GS_4_06: 24,40 CHA_PR_LIMITED_GS_4_03: 24,41 CHA_PR_LIMITED_GS_4_04: 24,42 CHA_WA_LIMITED_GS_4_07: 24,43 CHA_WI_LIMITED_GS_4_05: 24,44};45const shPool: RollChances = {46 CHA_PA_LIMITED_SH_4_02: 1,47 CHA_PA_LIMITED_SH_4_03: 3,48 CHA_WA_LIMITED_SH_4_01: 1,49 CHA_WI_LIMITED_SH_4_04: 3,50 CHA_WI_LIMITED_SH_4_05: 3,51 CHA_WI_LIMITED_SH_4_06: 3,52};53const HEROES_HIDDEN = [54 'legendary',55 'support',56 // 'secret'57];58type Puller = (count: number) => HeroForm[];59const PullValues = stringTuple('gg1', 'gg2', 'rz', 'sh', 'contract', 'gs');60type PullType = typeof PullValues[number];61const cmdArgs: CommandArguments = {62 type: {63 required: false,64 description: `Pull type. Can be one of ${PullValues.join(', ')}`,65 },66 count: {67 required: false,68 description: 'Contracts to pull. Defaults to 10 and capped at 20',69 }70};71// FIXME pull translations72export class PullCommand extends BaseCommand {73 readonly args = cmdArgs;74 readonly argsOrderMatters = true;75 readonly category = CommandCategory.MISC;76 readonly commandName = 'pull';77 readonly description = 'Simulate ingame contracts pull';78 readonly protected = false;79 private pulls?: Record<PullType, Puller>;80 private initPulls(): void {81 const forms = heroes82 .list()83 .filter(hero => hero.type && !HEROES_HIDDEN.includes(hero.type))84 .map(hero => (85 hero.forms.map((form) => {86 form.hero = hero;87 return form;88 })89 ))90 .flat();91 const secretForms = forms.filter(f => f.hero.type === 'secret' || f.hero.type === 'collab');92 const plainForms = forms.filter(f => f.hero.type !== 'secret' && f.hero.type !== 'collab');93 const secretFormsById = secretForms.reduce(94 (r, t) => { r[t.id] = t; return r; },95 {} as Record<string, HeroForm>96 );97 const sortedForms: Record<number | 'guaranteed', HeroForm[]> = {98 guaranteed: [],99 };100 for (const key of [3, 4, 5, 6]) {101 sortedForms[key] = plainForms.filter(form => (102 Number(form.star) === key103 ));104 }105 sortedForms.guaranteed = sortedForms[4].filter(f => f.hero.type === 'contract');106 const ggPull = (count: number, chances: RollChances): HeroForm[] => Array.from({ length: count })107 .map((_, idx) => {108 if ((1 + idx) % 10) {109 const grade = pickGrade({ 4: 0.17 }, 3);110 if (grade === 3) {111 return sortedForms[3][random(0, sortedForms[3].length - 1)];112 }113 const form = chancesRoll(chances);114 return secretFormsById[form];115 }116 const form = chancesRoll(chances);117 return secretFormsById[form];118 });119 this.pulls = {120 gg1: count => ggPull(count, gg1Pool),121 gg2: count => ggPull(count, gg2Pool),122 rz: count => ggPull(count, reZeroPool),123 sh: count => ggPull(count, shPool),124 contract: count => Array.from({ length: count })125 .map((_, idx) => {126 if ((1 + idx) % 10) {127 const grade = pickGrade();128 return sortedForms[grade][random(0, sortedForms[grade].length - 1)];129 }130 return sortedForms.guaranteed[random(0, sortedForms.guaranteed.length - 1)];131 }),132 gs: count => ggPull(count, gsPool),133 };134 }135 async run({ message, args: [rawType, rawCount] }: CommandPayload): Promise<Partial<CommandResult>> {136 let pullType: string;137 let pullCountString: string;138 if (typeof rawType === 'undefined' || !Number.isNaN(Number(rawType))) {139 pullCountString = rawType;140 pullType = 'contract';141 } else {142 pullCountString = rawCount;143 pullType = rawType;144 }145 if (this.pulls === undefined) {146 this.initPulls();147 }148 if (!(pullType in this.pulls!)) {149 await message.channel.send(`Unknown pull type. Can be ${PullValues.join(', ')}!`);150 return {151 statusCode: CommandResultCode.ENTITY_NOT_FOUND,152 target: 'pull',153 args: JSON.stringify({ pullType, pullCount: pullCountString }),154 };155 }156 const pullCount = makeInRange((Number(pullCountString) || 10), 1, 20);157 const pull = this.pulls![pullType as PullType](pullCount);158 const canvas = await makePullImage(pull);159 const chunks = formatPullChunks(pull);160 const embed = new MessageEmbed()161 .setAuthor(`${message.author.username}#${message.author.discriminator}`, message.author.avatarURL() || undefined)162 .setImage('attachment://pull.png');163 for (const chunk of chunks) {164 embed.addField('\u200b', chunk.join('\n'));165 }166 await message.channel.send({167 embed,168 files: [169 new MessageAttachment(await canvas.getBufferAsync('image/png'), 'pull.png')170 ]171 });172 return {173 statusCode: CommandResultCode.SUCCESS,174 target: 'pull',175 args: JSON.stringify({ pullType, pullCount }),176 };177 }178}...
messengerConfigService.js
Source:messengerConfigService.js
1const { disallow } = require('feathers-hooks-common');2const { Configuration } = require('@hpi-schul-cloud/commons');3const { ForbiddenError } = require('../../../errors/applicationErrors');4const { hasPermission } = require('../../../hooks');5const schoolMessengerOptions = ['messenger', 'studentsCanCreateMessengerRoom', 'messengerSchoolRoom'];6class MessengerConfigService {7 constructor(options) {8 this.options = options || {};9 }10 async setup(app) {11 this.app = app;12 }13 async find(params) {14 if (Configuration.get('FEATURE_MATRIX_MESSENGER_ENABLED') !== true) {15 throw new ForbiddenError('FEATURE_MATRIX_MESSENGER_ENABLED is disabled');16 }17 const school = await this.app.service('schools').get(params.route.schoolId);18 return Object.fromEntries(schoolMessengerOptions.map((option) => [option, school.features.includes(option)]));19 }20 async patch(_id, data, params) {21 if (Configuration.get('FEATURE_MATRIX_MESSENGER_ENABLED') !== true) {22 throw new ForbiddenError('FEATURE_MATRIX_MESSENGER_ENABLED is disabled');23 }24 const school = await this.app.service('schools').get(params.route.schoolId);25 // remove data unrelated to messenger26 Object.keys(data)27 .filter((key) => !schoolMessengerOptions.includes(key))28 .forEach((key) => delete data[key]);29 let [pushValues, pullValues] = Object.keys(data).reduce(30 (result, element) => {31 result[data[element] ? 0 : 1].push(element);32 return result;33 },34 [[], []]35 );36 pushValues = pushValues.filter((value) => !school.features.includes(value));37 pullValues = pullValues.filter((value) => school.features.includes(value));38 await Promise.all([39 this.app.service('schools').patch(school._id, { $push: { features: { $each: pushValues } } }, params),40 this.app.service('schools').patch(school._id, { $pull: { features: { $in: pullValues } } }, params),41 ]);42 return this.find(params);43 }44}45const messengerConfigService = new MessengerConfigService({});46const messengerConfigHooks = {47 before: {48 all: [],49 find: [],50 get: [disallow()],51 create: [disallow()],52 update: [disallow()],53 patch: [hasPermission('SCHOOL_EDIT', 'SCHOOL_CHAT_MANAGE')],54 remove: [disallow()],55 },56 after: {},57};...
gpio.js
Source:gpio.js
1const Gpio = require('onoff').Gpio;2const { spawnSync } = require('child_process');3let pins = {}4const pullValues = {5 'up': 'pu',6 'down': 'pd',7 'none': 'pn'8}9module.exports.init = function(num, direction, edge, pull) {10 if (num in pins) {11 throw new Error(`ERROR: pin ${num} already initialized`)12 }13 if (pull !== null) {14 if (!(pull in pullValues)) {15 throw new Error(`ERROR: pin pull value ${pull} is not correct, should be one of ${Object.keys(pullValues).join(', ')}`)16 }17 const ret = spawnSync('raspi-gpio', ['set', num, pullValues[pull]])18 if (ret.status !== 0) {19 console.log(ret.stdout.toString(), ret.stderr.toString())20 throw new Error(`ERROR: can't initalizing pin ${num} with pulling ${pull}, returned ${ret}`)21 }22 }23 pins[num] = new Gpio(num, direction, edge);24}25module.exports.monitor = function(num, callback) {26 if (!(num in pins)) {27 throw new Error(`ERROR: pin ${num} not initialized`)28 }29 const pin = pins[num]30 function watchCallback(err, value) {31 console.log(`watch : value=${value}, err=${err}`)32 if (err) {33 throw err;34 }35 callback(value)36 }37 pin.watch(watchCallback);38 return () => {39 pin.unwatch(watchCallback);40 }41}42module.exports.write = function(num, value) {43 if (!(num in pins)) {44 throw new Error(`ERROR: pin ${num} not initialized`)45 }46 console.log(`write pin ${num} value ${value}`)47 const pin = pins[num]48 pin.writeSync(value)49}50module.exports.cleanup = function() {51 console.log("gpio : cleaning up before shutdown...")52 for (const [num, pin] of Object.entries(pins)) {53 pin.unexport()54 }55 pins = {}...
Using AI Code Generation
1const pullValues = require('fast-check-monorepo').pullValues;2pullValues(10, 100);3const pullValues = require('fast-check-monorepo').pullValues;4pullValues(10, 100);5const pullValues = require('fast-check-monorepo').pullValues;6pullValues(10, 100);7const pullValues = require('fast-check-monorepo').pullValues;8pullValues(10, 100);9const pullValues = require('fast-check-monorepo').pullValues;10pullValues(10, 100);11const pullValues = require('fast-check-monorepo').pullValues;12pullValues(10, 100);13const pullValues = require('fast-check-monorepo').pullValues;14pullValues(10, 100);15const pullValues = require('fast-check-monorepo').pullValues;16pullValues(10, 100);17const pullValues = require('fast-check-monorepo').pullValues;18pullValues(10, 100);19const pullValues = require('fast-check-monorepo').pullValues;20pullValues(10, 100);21const pullValues = require('fast-check-monorepo').pullValues;22pullValues(10, 100);23const pullValues = require('fast-check-monorepo').pullValues;24pullValues(10, 100);
Using AI Code Generation
1const x = pullValues([1, 2, 3, 4, 5, 6, 7, 8, 9, 10], 5);2console.log(x);3const x = pullValues([1, 2, 3, 4, 5, 6, 7, 8, 9, 10], 5);4console.log(x);5const x = pullValues([1, 2, 3, 4, 5, 6, 7, 8, 9, 10], 5);6console.log(x);7const x = pullValues([1, 2, 3, 4, 5, 6, 7, 8, 9, 10], 5);8console.log(x);9const x = pullValues([1, 2, 3, 4, 5, 6, 7, 8, 9, 10], 5);10console.log(x);11const x = pullValues([1, 2, 3, 4, 5, 6, 7, 8, 9, 10], 5);12console.log(x);13const x = pullValues([1, 2, 3, 4, 5, 6, 7, 8, 9, 10], 5);14console.log(x);15const x = pullValues([1, 2, 3, 4,
Using AI Code Generation
1import { pullValues } from 'fast-check-monorepo'2console.log(pullValues(myArray, 3))3console.log(pullValues(myArray2, 3))4console.log(pullValues(myArray3, 3))5console.log(pullValues(myArray4, 3))6console.log(pullValues(myArray5, 3))7console.log(pullValues(myArray6, 3))
Using AI Code Generation
1const { pullValues } = require('fast-check');2const myGenerator = () => {3}4const myValues = pullValues(myGenerator, 1000);5const { pullValues } = require('fast-check');6const myGenerator = () => {7}8const myValues = pullValues(myGenerator, 1000);9const { pullValues } = require('fast-check');10const myGenerator = () => {11}12const myValues = pullValues(myGenerator, 1000);13const { pullValues } = require('fast-check');14const myGenerator = () => {15}16const myValues = pullValues(myGenerator, 1000);17const { pullValues } = require('fast-check');18const myGenerator = () => {19}20const myValues = pullValues(myGenerator, 1000);21const { pullValues } = require('fast-check');22const myGenerator = () => {23}24const myValues = pullValues(myGenerator, 1000);25const { pullValues } = require('fast-check');26const myGenerator = () => {27}28const myValues = pullValues(myGenerator, 1000);
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!!