Best JavaScript code snippet using fast-check-monorepo
JsonParser.ts
Source:JsonParser.ts
1///<reference path='refs.ts'/>2module TDev.RT {3 export class JsonParser4 {5 /// JSON parser trying to be more lenient than real JSON, mimicking what .NET does. This means we accept6 /// - single quoted or double quoted keys7 /// - unquoted keys, as long as they are letter, digit, _, -, ., +, -8 /// NOTE this does not match .NET exactly, as .NET uses Unicode characterization of IsNumberOrDigit.9 ///10 static parse(s: string, log? : (msg:string) => void) : JsonObject11 {12 var index = 0;13 var current = s[0];14 var onerror = function (m:string) :void {15 throw { message: m }16 };17 var cnext = function (expected:string) {18 if (expected != current) {19 onerror(lf("got `{0}`, but expected `{1}`", current, expected));20 }21 current = s[++index];22 return current;23 };24 var parseNumber = function () : number {25 var value: number;26 var sofar = "";27 if (current === "-") {28 sofar = "-";29 cnext("-");30 }31 while (current >= "0" && current <= "9") {32 sofar += current;33 cnext(current);34 }35 if (current === ".") {36 sofar += current;37 while (cnext(current) && current >= "0" && current <= "9") {38 sofar += current;39 }40 }41 if (current === "E" || current === "e") {42 sofar += current;43 cnext(current);44 if (current === "-" || current === "+") {45 sofar += current;46 cnext(current);47 }48 while (current >= "0" && current <= "9") {49 sofar += current;50 cnext(current);51 }52 }53 value = +sofar;54 if (isNaN(value)) {55 onerror(lf("not a number"));56 }57 return value;58 }59 var getQuoteChar = function() : string {60 if (current == '"') return current;61 if (current == "'") return current;62 onerror(lf("missing quote for string"));63 }64 var parseString = function () {65 var quote = getQuoteChar();66 var escapedChar=false;67 var sofar = "";68 while (cnext(current)) {69 if (current === "\\") {70 if (escapedChar) {71 sofar += '\\';72 escapedChar = false;73 }74 else {75 escapedChar = true;76 }77 continue;78 }79 if (escapedChar) {80 escapedChar = false;81 if (current === '"' || current === "'" || current === "/") {82 sofar += current;83 }84 else if (current === "b") {85 sofar += "\b";86 }87 else if (current === "f") {88 sofar += "\f";89 }90 else if (current === "n") {91 sofar += "\n";92 }93 else if (current === "r") {94 sofar += "\r";95 }96 else if (current === "t") {97 sofar += "\t";98 }99 else if (current === "u") {100 var value = 0;101 for (var i = 0; i < 4; i++) {102 var digit = parseInt(cnext(current), 16);103 if (!isFinite(digit)) {104 break;105 }106 value = value * 16 + digit;107 }108 sofar += String.fromCharCode(value);109 }110 else {111 onerror(lf("bad escaped char: {0}", current));112 }113 }114 else {115 if (current === quote) {116 cnext(quote);117 return sofar;118 }119 sofar += current;120 }121 }122 onerror(lf("non-terminated string"));123 }124 var skipWhiteSpace = function () {125 while (current && current <= " ") {126 cnext(current);127 }128 }129 var parsePrimitiveToken = function() {130 var sofar = "";131 while (current) {132 if (current >= "0" && current <= "9" || current >= "a" && current <= "z" || current >= "A" && current <= "Z" || current === "." || current === "-" || current === "_" || current === "+") {133 sofar += current;134 cnext(current);135 }136 else {137 break;138 }139 }140 return sofar;141 }142 var parsePrimitive = function () : any {143 switch (current) {144 case 't':145 cnext("t");146 cnext("r");147 cnext("u");148 cnext("e");149 return true;150 case 'f':151 cnext("f");152 cnext("a");153 cnext("l");154 cnext("s");155 cnext("e");156 return false;157 case 'n':158 cnext("n");159 cnext("u");160 cnext("l");161 cnext("l");162 return null;163 }164 onerror("expected true, false, or null at \n"165 +" " + s.slice(Math.max(0, index - 20), index) + "\n"166 +" ---->" + s.slice(index + 1, Math.min(s.length, index + 20)));167 }168 var parseArray : () => any[] = function() {169 var result = [];170 cnext('[');171 skipWhiteSpace();172 if (current === ']') {173 cnext(']');174 return result;175 }176 while (current) {177 result.push(parseValue());178 skipWhiteSpace();179 if (current === ']') {180 cnext(']');181 return result;182 }183 cnext(",");184 skipWhiteSpace();185 }186 onerror(lf("incomplete array"));187 return result;188 }189 var parseValue : () => any = function (){190 skipWhiteSpace();191 switch (current) {192 case '{': return parseObject();193 case '[': return parseArray();194 case '"':195 case "'":196 return parseString();197 case '-':198 return parseNumber();199 default:200 if (current >= '0' && current <= '9') return parseNumber();201 else return parsePrimitive();202 }203 }204 var parseObject = function (): any {205 var result = {};206 cnext("{");207 skipWhiteSpace();208 if (current === "}") {209 cnext("}");210 return result;211 }212 var key: string;213 while (current) {214 if (current === "'" || current === '"') {215 key = parseString();216 }217 else {218 key = parsePrimitiveToken();219 }220 if (key === null || key === undefined) {221 onerror(lf("bad key"));222 }223 skipWhiteSpace();224 cnext(":");225 result[key] = parseValue();226 skipWhiteSpace();227 if (current === "}") {228 cnext("}");229 return result;230 }231 cnext(",");232 skipWhiteSpace();233 }234 onerror(lf("incomplete object"));235 }236 try {237 return JsonObject.wrap(JSON.parse(s))238 } catch (e) {239 try {240 var value = parseValue();241 skipWhiteSpace();242 if (current) {243 onerror(lf("illegal json value"));244 }245 var js = JsonObject.wrap(value);246 return js;247 }248 catch(e)249 {250 if (log)251 log(lf("error parsing json {0} {1}", s.length > 100 ? s.slice(0,100) + "..." : s, e.message));252 return undefined;253 }254 }255 }256 }...
search.js
Source:search.js
1function dfs(init, adj, goalTest) {2 let stack = [init];3 let visited = {init: null};4 while(stack.length > 0) {5 // Apply DFS strategy to expand node6 let cnode = stack.pop();7 // Check if cnode is the goal8 if (goalTest(cnode)) {9 return pathToGoal(init, cnode, visited);10 }11 // Iterate on the list of adjacent nodes12 for (let cnext of adj(cnode)) {13 if(!(cnext in visited)) {14 visited[cnext] = cnode;15 stack.push(cnext);16 }17 }18 }19 return null;20}21function bfs(init, adj, goalTest) {22 let queue = [init];23 let visited = {init: null};24 while(queue.length > 0) {25 // Apply DFS strategy to expand node26 let cnode = queue.shift();27 // Check if cnode is the goal28 if (goalTest(cnode)) {29 return pathToGoal(init, cnode, visited);30 }31 // Iterate on the list of adjacent nodes32 for (let cnext of adj(cnode)) {33 if(!(cnext in visited)) {34 visited[cnext] = cnode;35 queue.push(cnext);36 }37 }38 }39 return null;40}41function pathToGoal(init, goal, visited) {42 let path = [goal];43 let n = goal;44 while(n != init) {45 n = visited[n];46 path.push(n);47 }48 return path;...
Using AI Code Generation
1const fc = require('fast-check');2fc.assert(fc.property(fc.integer(), fc.integer(), fc.integer(), (a, b, c) => {3 if (a > b) {4 return;5 }6 if (b > c) {7 return;8 }9 if (a > c) {10 fc.cNext('a > c');11 }12}));
Using AI Code Generation
1const fc = require('fast-check');2const { cNext } = require('fast-check-monorepo');3fc.assert(4 fc.property(fc.integer(), (n) => {5 const next = cNext(n);6 return next === n + 1;7 })8);9const fc = require('fast-check');10const { cNext } = require('fast-check-monorepo');11fc.assert(12 fc.property(fc.integer(), (n) => {13 const next = cNext(n);14 return next === n + 1;15 })16);
Using AI Code Generation
1const fc = require('fast-check');2const {cNext} = require('fast-check/lib/check/arbitrary/CNextArbitrary');3const {cNextInt} = require('fast-check/lib/check/arbitrary/CNextArbitrary');4const {cNextString} = require('fast-check/lib/check/arbitrary/CNextArbitrary');5const {cNextBool} = require('fast-check/lib/check/arbitrary/CNextArbitrary');6const {cNextDate} = require('fast-check/lib/check/arbitrary/CNextArbitrary');7const {cNextFloat} = require('fast-check/lib/check/arbitrary/CNextArbitrary');8const {cNextDouble} = require('fast-check/lib/check/arbitrary/CNextArbitrary');9const {cNextChar} = require('fast-check/lib/check/arbitrary/CNextArbitrary');10const {cNextUint8Array} = require('fast-check/lib/check/arbitrary/CNextArbitrary');11const {cNextUint32Array} = require('fast-check/lib/check/arbitrary/CNextArbitrary');12const {cNextUint16Array} = require('fast-check/lib/check/arbitrary/CNextArbitrary');13const {cNextUint8ClampedArray} = require('fast-check/lib/check/arbitrary/CNextArbitrary');14const {cNextInt8Array} = require('fast-check/lib/check/arbitrary/CNextArbitrary');15const {cNextInt16Array} = require('fast-check/lib/check/arbitrary/CNextArbitrary');16const {cNextInt32Array} = require('fast-check/lib/check/arbitrary/CNextArbitrary');17const {cNextFloat32Array} = require('fast-check/lib/check/arbitrary/CNextArbitrary');18const {cNextFloat64Array} = require('fast-check/lib/check/arbitrary/CNextArbitrary');19const {cNextBigInt64Array} = require('fast-check/lib/check/arbitrary/CNextArbitrary');20const {cNextBigUint64Array} = require('fast-check/lib/check/arbitrary/CNextArbitrary');21const {cNextUint8} = require('fast-check/lib/check/arbitrary/CNextArbitrary');22const {cNextUint16} = require('fast-check/lib/check/arbitrary/CNextArbitrary');23const {cNextUint32} = require('fast-check/lib/check/arbitrary/CNextArbitrary');24const {cNextInt8} = require('fast-check/lib/check/arbitrary/CNextArbitrary
Using AI Code Generation
1const { cNext } = require('fast-check-monorepo');2const fc = require('fast-check');3const c = cNext(fc);4const { property } = fc;5const { assert } = require('chai');6const add = (a, b) => a + b;7const sub = (a, b) => a - b;8describe('Addition', () => {9 it('should be commutative', () => {10 property(c.int, c.int, (a, b) => {11 assert.equal(add(a, b), add(b, a));12 });13 });14 it('should be associative', () => {15 property(c.int, c.int, c.int, (a, b, c) => {16 assert.equal(add(a, add(b, c)), add(add(a, b), c));17 });18 });19 it('should be distributive', () => {20 property(c.int, c.int, c.int, (a, b, c) => {21 assert.equal(add(a, sub(b, c)), sub(add(a, b), add(a, c)));22 });23 });24});25const { cNext } = require('fast-check-monorepo');26const fc = require('fast-check');27const c = cNext(fc);28const { property } = fc;29const { assert } = require('chai');30const add = (a, b) => a + b;31const sub = (a, b) => a - b;32describe('Addition', () => {33 it('should be commutative', () => {34 property(c.int, c.int, (a, b) => {35 assert.equal(add(a, b), add(b, a));36 });37 });38 it('should be associative', () => {39 property(c.int, c.int, c.int, (a, b, c) => {40 assert.equal(add(a, add(b, c)), add(add(a, b), c));41 });42 });43 it('should be distributive', () => {44 property(c.int, c.int, c.int, (a, b, c) => {45 assert.equal(add(a, sub(b, c)), sub(add(a, b), add(a, c)));46 });47 });48});
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!!