Best JavaScript code snippet using fast-check-monorepo
index.js
Source:index.js
1/**2 * Fetch one or more Assets matching a query string using full-text search.3 */4const ensure = require("#tvf-ensure");5const isUnlockedSql = require("../../../common/isUnlockedSql");6const augmentWithFragmentsAndRemoveIds = require("./augmentWithFragmentsAndRemoveIds");7const getAssetFormatTitle = require("../../../common/getAssetFormatTitle");8const getAllowedTypes = require("../common/getAllowedTypes");9const augmentWithUploadedExtractsAndRemoveIds = require("./augmentWithUploadedExtractsAndRemoveIds");10const allowedFilterTypes = getAllowedTypes.filterTypes;11const niceLanguagesMap = getAllowedTypes.niceLanguagesMap;12const getNiceLanguage = (code) => {13 return niceLanguagesMap[code] ? niceLanguagesMap[code] : code;14};15module.exports = async function (params, ctx) {16 // Make sure provided query, offset and limit are sensible.17 ensure.nonNegativeInteger(ctx, params.offset, "Offset");18 if (params.query) {19 ensure.nonEmptyStr(ctx, params.query, "Query");20 }21 if (!(params.limit && typeof params.limit === "number" && Math.floor(params.limit) == params.limit && params.limit > 0 && params.limit <= 100)) {22 ctx.throw(400, "Valid limit not provided");23 }24 // Searching requires the user to be logged in - make sure they are.25 const userSessionData = await ctx.getSessionData();26 const schoolId = parseInt(userSessionData ? userSessionData.school_id : 0, 10) || 0;27 const userId = parseInt(userSessionData ? userSessionData.user_id : 0, 10) || 0;28 const isLoggedIn = userSessionData ? true : false;29 const filterBy = {30 allCopies: false,31 myCopies: false,32 //mySchoolLibrary: false,33 unlockBooks: false,34 allExtracts: false,35 };36 const activeFilters = Object.create(null);37 activeFilters.subject = null;38 const filterRequest = {};39 if (params.filter && typeof params.filter === "object") {40 if (Array.isArray(params.filter.subject)) {41 activeFilters.subject = params.filter.subject;42 }43 if (Array.isArray(params.filter.misc)) {44 for (const filter of params.filter.misc) {45 switch (filter) {46 case "all_copies":47 filterBy.allCopies = true;48 break;49 case "my_copies":50 filterBy.myCopies = true;51 break;52 case "unlock_books":53 filterBy.unlockBooks = true;54 break;55 // case 'my_school_library':56 // filterBy.mySchoolLibrary = true;57 // break;58 case "all_extracts":59 filterBy.allExtracts = true;60 break;61 }62 }63 }64 for (const allowedFilterType in allowedFilterTypes) {65 if (params.filter[allowedFilterType]) {66 filterRequest[allowedFilterType] = params.filter[allowedFilterType];67 }68 }69 }70 let queryValues = [];71 let queryWhereClause;72 let queryOrderByClause;73 let queryJoinClause = "";74 //For filterBy query and joins for My Library75 const extractJoin = `INNER JOIN extract ON extract.asset_id = asset.id`;76 const allCopiesExtractWhere = `(extract.school_id = ${schoolId})`;77 const myCopiesExtractWhere = `(extract.user_id = ${userId})`;78 const assetSchoolInfoJoin = `LEFT JOIN asset_school_info ON asset_school_info.asset_id = asset.id AND asset_school_info.school_id = ${schoolId}`;79 const unlockBookWhere = `(${isUnlockedSql(isLoggedIn)} = TRUE)`;80 const assetUserUploadJoin = `INNER JOIN asset_user_upload ON asset_user_upload.asset_id = asset.id `;81 {82 const joinClauses = Object.create(null);83 joinClauses.asset_user_upload = "LEFT JOIN asset_user_upload ON asset_user_upload.asset_id = asset.id";84 joinClauses.cla_user = "LEFT JOIN cla_user ON cla_user.id = asset_user_upload.user_id";85 const orderBy = [];86 const whereClauses = ["asset.active", "asset.is_ep", "(asset.date_system_created IS NOT NULL OR cla_user.school_id = " + schoolId + ")"];87 if (params.query) {88 const idx = queryValues.push(params.query);89 whereClauses.push(`(weighted_tsv @@ plainto_tsquery($${idx}))`);90 orderBy.push(`ts_rank_cd(weighted_tsv, plainto_tsquery($${idx})) DESC`);91 }92 if (activeFilters.subject) {93 const subjectValues = [];94 for (const subject of activeFilters.subject) {95 if (subject.match(/^[A-Z]+$/)) {96 if (subject.length === 3) {97 subjectValues.push(`(asset_subject.third_level_subject_code = '${subject}')`);98 } else {99 subjectValues.push(`(asset_subject.fourth_level_subject_code = '${subject}')`);100 }101 }102 }103 if (subjectValues.length) {104 joinClauses.asset_subject = `INNER JOIN asset_subject ON asset.id = asset_subject.asset_id`;105 whereClauses.push("(" + subjectValues.join(" OR ") + ")");106 }107 }108 if (filterBy.unlockBooks) {109 joinClauses.asset_school_info = assetSchoolInfoJoin;110 whereClauses.push(unlockBookWhere);111 }112 if (filterBy.allCopies) {113 joinClauses.extract = extractJoin;114 whereClauses.push(allCopiesExtractWhere);115 }116 if (filterBy.myCopies) {117 if (!filterBy.allCopies) {118 joinClauses.extract = extractJoin;119 }120 whereClauses.push(myCopiesExtractWhere);121 }122 if (filterBy.allExtracts) {123 joinClauses.asset_user_upload = assetUserUploadJoin;124 // whereClauses.push(myCopiesExtractWhere);125 }126 if (!joinClauses.asset_school_info) {127 joinClauses.asset_school_info = `LEFT JOIN asset_school_info128 ON asset_school_info.asset_id = asset.id129 AND asset_school_info.school_id = ${schoolId}130 `;131 }132 if (userId) {133 joinClauses.asset_user_info = `LEFT JOIN asset_user_info ON asset_user_info.asset_id = asset.id AND asset_user_info.user_id = ${userId}`;134 }135 for (const filterRequestType in filterRequest) {136 if (!filterRequest.hasOwnProperty(filterRequestType)) {137 continue;138 }139 const filterRequestValues = filterRequest[filterRequestType];140 const subWhereClauses = [];141 if (filterRequestType != "publisher" && filterRequestType != "format") {142 joinClauses[`filter_${filterRequestType}`] = `143 LEFT JOIN asset_${filterRequestType} af_${filterRequestType} ON144 asset.id = af_${filterRequestType}.asset_id145 `;146 for (const value of filterRequestValues) {147 const idx1 = queryValues.push(value);148 subWhereClauses.push(`(af_${filterRequestType}.${filterRequestType} = $${idx1})`);149 }150 whereClauses.push("(" + subWhereClauses.join(" OR ") + ")");151 }152 if (filterRequestType === "publisher") {153 let publisherWhereClause = "";154 publisherWhereClause += "asset.publisher_name_log IN (";155 let i = 1;156 for (const value of filterRequestValues) {157 const idx1 = queryValues.push(value);158 if (filterRequestValues.length > i) {159 publisherWhereClause += `$${idx1} ,`;160 } else {161 publisherWhereClause += `$${idx1}`;162 }163 i++;164 }165 publisherWhereClause += `)`;166 //whereClauses.push('(' + subWhereClauses.join(' AND ') + ')');167 whereClauses.push("(" + publisherWhereClause + ")");168 }169 if (filterRequestType === "format") {170 let contentFormWhereClause = "";171 contentFormWhereClause += "content_form IN (";172 let i = 1;173 for (const value of filterRequestValues) {174 const idx1 = queryValues.push(value);175 if (filterRequestValues.length > i) {176 contentFormWhereClause += `$${idx1} ,`;177 } else {178 contentFormWhereClause += `$${idx1}`;179 }180 i++;181 }182 contentFormWhereClause += `)`;183 //whereClauses.push('(' + subWhereClauses.join(' AND ') + ')');184 whereClauses.push("(" + contentFormWhereClause + ")");185 }186 }187 queryWhereClause = whereClauses.length > 0 ? "WHERE " + whereClauses.join(" AND ") : "";188 orderBy.push("id DESC");189 queryOrderByClause = orderBy.join(",");190 queryJoinClause = Object.values(joinClauses).join(" ");191 }192 try {193 // Fetch the unfiltered result count to help the front-end paginate the results.194 let unfilteredCount;195 {196 const text = `197 SELECT198 COUNT(DISTINCT asset.id) AS _count_199 FROM200 asset201 ${queryJoinClause}202 ${queryWhereClause}203 `;204 const results = await ctx.appDbQuery(text, queryValues);205 unfilteredCount = parseInt(results.rows[0]._count_, 10);206 }207 // Assuming some results are available, fetch their Asset IDs.208 let results;209 if (unfilteredCount > 0) {210 const text = `211 SELECT212 id,213 title,214 content_form,215 publisher,216 authors,217 is_unlocked,218 isbn13,219 pdf_isbn13,220 sub_title,221 publication_date,222 edition,223 subject_codes_log,224 copies_count,225 is_favorite,226 auto_unlocked,227 can_copy_in_full,228 is_system_asset229 FROM (230 SELECT231 DISTINCT ON (asset.id)232 asset.id AS id,233 asset.content_form AS content_form,234 asset.title AS title,235 asset.publisher_name_log AS publisher,236 asset.authors_log AS authors,237 ${isUnlockedSql(isLoggedIn)} AS is_unlocked,238 asset.isbn13 AS isbn13,239 asset.pdf_isbn13 AS pdf_isbn13,240 asset.sub_title AS sub_title,241 asset.publication_date AS publication_date,242 asset.edition AS edition,243 asset.subject_codes_log AS subject_codes_log,244 asset.weighted_tsv AS weighted_tsv,245 CASE WHEN ${userId} != 0 THEN (SELECT COUNT(*) FROM extract WHERE asset_id = asset.id and school_id = ${schoolId} )246 ELSE 0247 END AS copies_count,248 ${userId ? "COALESCE(asset_user_info.is_favorite, FALSE)" : "FALSE"} AS is_favorite,249 asset.auto_unlocked AS auto_unlocked,250 asset.can_copy_in_full AS can_copy_in_full,251 asset.date_system_created IS NOT NULL AS is_system_asset252 FROM asset253 ${queryJoinClause}254 ${queryWhereClause}255 ORDER BY256 asset.id DESC257 ) AS sub258 ORDER BY259 ${queryOrderByClause}260 LIMIT261 ${params.limit}262 OFFSET263 ${params.offset}264 `;265 results = (await ctx.appDbQuery(text, queryValues)).rows;266 if (params.query && results.length) {267 await augmentWithFragmentsAndRemoveIds(ctx.appDbQuery.bind(ctx), results, params.query);268 }269 await augmentWithUploadedExtractsAndRemoveIds(ctx.appDbQuery.bind(ctx), results, params.query);270 } else {271 results = [];272 }273 //Get filter data with count274 let queryFilterValues = [];275 let queryFilterWhereClause;276 let queryFilterJoinClause = "";277 let resultFilter = [];278 //All filter counts data279 let resultFilterCount = 0;280 {281 const joinClauses = Object.create(null);282 const orderBy = [];283 const whereClauses = ["asset.active", "asset.is_ep"];284 if (params.query) {285 const idx = queryFilterValues.push(params.query);286 whereClauses.push(`(weighted_tsv @@ plainto_tsquery($${idx}))`);287 orderBy.push(`ts_rank_cd(weighted_tsv, plainto_tsquery($${idx})) DESC`);288 }289 if (filterBy.unlockBooks) {290 joinClauses.asset_school_info = assetSchoolInfoJoin;291 whereClauses.push(unlockBookWhere);292 }293 if (filterBy.allCopies) {294 joinClauses.extract = extractJoin;295 whereClauses.push(allCopiesExtractWhere);296 }297 if (filterBy.myCopies) {298 if (!filterBy.allCopies) {299 joinClauses.extract = extractJoin;300 }301 whereClauses.push(myCopiesExtractWhere);302 }303 if (filterBy.allExtracts) {304 joinClauses.assetUserUpload = assetUserUploadJoin;305 }306 if (activeFilters.subject) {307 const subjectValues = [];308 for (const subject of activeFilters.subject) {309 if (subject.match(/^[A-Z]+$/)) {310 if (subject.length === 3) {311 subjectValues.push(`(asset_subject.third_level_subject_code = '${subject}')`);312 } else {313 subjectValues.push(`(asset_subject.fourth_level_subject_code = '${subject}')`);314 }315 }316 }317 if (subjectValues.length) {318 whereClauses.push("(" + subjectValues.join(" OR ") + ")");319 }320 }321 joinClauses.asset_subject = `LEFT JOIN asset_subject ON asset.id = asset_subject.asset_id`;322 joinClauses.third_level_subject = `LEFT JOIN third_level_subject ON third_level_subject.code = asset_subject.third_level_subject_code`;323 joinClauses.fourth_level_subject = `LEFT JOIN fourth_level_subject ON fourth_level_subject.code = asset_subject.fourth_level_subject_code`;324 for (const filterRequestType in allowedFilterTypes) {325 if (filterRequestType != "publisher" && filterRequestType != "format") {326 joinClauses[`filter_${filterRequestType}`] = `327 LEFT JOIN asset_${filterRequestType} af_${filterRequestType} ON328 asset.id = af_${filterRequestType}.asset_id329 `;330 }331 }332 for (const filterRequestType in filterRequest) {333 const filterRequestValues = filterRequest[filterRequestType];334 const subWhereClauses = [];335 if (filterRequestType != "publisher" && filterRequestType != "format") {336 for (const value of filterRequestValues) {337 const idx1 = queryFilterValues.push(value);338 subWhereClauses.push(`(af_${filterRequestType}.${filterRequestType} = $${idx1})`);339 }340 whereClauses.push("(" + subWhereClauses.join(" OR ") + ")");341 }342 if (filterRequestType === "publisher") {343 let publisherWhereClause = "";344 publisherWhereClause += "asset.publisher_name_log IN (";345 let i = 1;346 for (const value of filterRequestValues) {347 const idx2 = queryFilterValues.push(value);348 if (filterRequestValues.length > i) {349 publisherWhereClause += `$${idx2} ,`;350 } else {351 publisherWhereClause += `$${idx2}`;352 }353 i++;354 }355 publisherWhereClause += `)`;356 //whereClauses.push('(' + subWhereClauses.join(' AND ') + ')');357 whereClauses.push("(" + publisherWhereClause + ")");358 }359 if (filterRequestType === "format") {360 let contentFormWhereClause = "";361 contentFormWhereClause += "content_form IN (";362 let i = 1;363 for (const value of filterRequestValues) {364 const idx2 = queryFilterValues.push(value);365 if (filterRequestValues.length > i) {366 contentFormWhereClause += `$${idx2} ,`;367 } else {368 contentFormWhereClause += `$${idx2}`;369 }370 i++;371 }372 contentFormWhereClause += `)`;373 //whereClauses.push('(' + subWhereClauses.join(' AND ') + ')');374 whereClauses.push("(" + contentFormWhereClause + ")");375 }376 }377 queryFilterWhereClause = whereClauses.length > 0 ? "WHERE " + whereClauses.join(" AND ") : "";378 orderBy.push("id DESC");379 queryOrderByClause = orderBy.join(",");380 queryFilterJoinClause = Object.values(joinClauses).join(" ");381 }382 // count_unlockbooks383 let additionalJoin = "";384 let additionalQuery = "";385 {386 let countUnlockBooks = 0;387 {388 if (queryFilterJoinClause.indexOf(assetSchoolInfoJoin) == -1) {389 additionalJoin = " " + assetSchoolInfoJoin;390 }391 if (queryFilterWhereClause.indexOf(unlockBookWhere) == -1) {392 additionalQuery = " AND " + unlockBookWhere;393 }394 let unlockBookQueryJoin = queryFilterJoinClause.replace(extractJoin, " ");395 let queryWithoutALLCopies = queryFilterWhereClause.replace(" AND " + allCopiesExtractWhere, " ");396 let unlockQueryWhere = queryWithoutALLCopies.replace(" AND " + myCopiesExtractWhere, " ");397 const text = `398 SELECT399 COUNT(DISTINCT asset.id) AS count_unlockbooks400 FROM401 asset402 ${unlockBookQueryJoin}403 ${additionalJoin}404 ${unlockQueryWhere}405 ${additionalQuery}406 `;407 const results = await ctx.appDbQuery(text, queryFilterValues);408 countUnlockBooks = parseInt(results.rows[0].count_unlockbooks, 10);409 }410 //for All Copies411 let countAllCopies = 0;412 {413 additionalJoin = "";414 additionalQuery = "";415 if (queryFilterJoinClause.indexOf(extractJoin) == -1) {416 additionalJoin = " " + extractJoin;417 }418 if (queryFilterWhereClause.indexOf(allCopiesExtractWhere) == -1) {419 additionalQuery = " AND " + allCopiesExtractWhere;420 }421 //replace with My copies join and where condition422 let queryWithoutMyCopiesWhere = queryFilterWhereClause.replace(" AND " + myCopiesExtractWhere, " ");423 const text = `424 SELECT425 COUNT(DISTINCT asset.id) AS count_allcopies426 FROM427 asset428 ${queryFilterJoinClause}429 ${additionalJoin}430 ${queryWithoutMyCopiesWhere}431 ${additionalQuery}432 `;433 const results = await ctx.appDbQuery(text, queryFilterValues);434 countAllCopies = parseInt(results.rows[0].count_allcopies, 10);435 }436 //Count my copies437 let countMyCopies = 0;438 {439 additionalJoin = "";440 additionalQuery = "";441 if (queryFilterJoinClause.indexOf(extractJoin) == -1) {442 additionalJoin = " " + extractJoin;443 }444 if (queryFilterWhereClause.indexOf(myCopiesExtractWhere) == -1) {445 additionalQuery = " AND " + myCopiesExtractWhere;446 }447 const text = `448 SELECT449 COUNT(DISTINCT asset.id) AS count_mycopies450 FROM451 asset452 ${queryFilterJoinClause}453 ${additionalJoin}454 ${queryFilterWhereClause}455 ${additionalQuery}456 `;457 const results = await ctx.appDbQuery(text, queryFilterValues);458 countMyCopies = parseInt(results.rows[0].count_mycopies, 10);459 }460 //All Filter counts data461 {462 //replace with My copies join and where condition463 let queryJoinWithoutExtract = queryFilterJoinClause.replace(extractJoin, " ");464 let queryJoinWithoutExtractWhere = queryFilterWhereClause.replace(" AND " + allCopiesExtractWhere, " ");465 //replace with My copies join and where condition466 let myCopiesQueryJoin = queryJoinWithoutExtract.replace(extractJoin, " ");467 let myCopiesQueryWhere = queryJoinWithoutExtractWhere.replace(" AND " + myCopiesExtractWhere, " ");468 //replace with My institution library join and where condition469 myCopiesQueryJoin = myCopiesQueryJoin.replace(assetSchoolInfoJoin, " ");470 myCopiesQueryWhere = myCopiesQueryWhere.replace(" AND " + unlockBookWhere, " ");471 const text = `472 SELECT473 COUNT(DISTINCT asset.id) AS count_all474 FROM475 asset476 ${myCopiesQueryJoin}477 ${myCopiesQueryWhere}478 `;479 const results = await ctx.appDbQuery(text, queryFilterValues);480 resultFilterCount = parseInt(results.rows[0].count_all, 10);481 }482 //Count user uploaded extracts483 let countAllExtracts = 0;484 {485 additionalJoin = "";486 additionalQuery = "";487 if (queryFilterJoinClause.indexOf(assetUserUploadJoin) == -1) {488 additionalJoin = " " + assetUserUploadJoin;489 }490 const text = `491 SELECT492 COUNT(DISTINCT asset.id) AS count_allextracts493 FROM494 asset495 ${queryFilterJoinClause}496 ${additionalJoin}497 ${queryFilterWhereClause}498 ${additionalQuery}499 `;500 const results = await ctx.appDbQuery(text, queryFilterValues);501 countAllExtracts = parseInt(results.rows[0].count_allextracts, 10);502 }503 const copiesData = [];504 if (countAllCopies > 0) {505 copiesData.push({ id: "all_copies", title: "All Copies", count: countAllCopies });506 }507 if (countUnlockBooks > 0) {508 copiesData.push({ id: "unlock_books", title: "Unlocked Content", count: countUnlockBooks });509 }510 if (countMyCopies > 0) {511 copiesData.push({ id: "my_copies", title: "My copies only", count: countMyCopies });512 }513 if (countAllExtracts > 0) {514 copiesData.push({ id: "all_extracts", title: "All Extracts", count: countAllExtracts });515 }516 resultFilter.push({517 id: "misc",518 title: "My Library",519 data: copiesData,520 });521 }522 // bind subjects523 let subjectsThirdLevelData = {};524 {525 const thirdLevelSubjects = (526 await ctx.appDbQuery(527 `528 SELECT529 third_level_subject.code AS id,530 third_level_subject.name AS title,531 COUNT(DISTINCT asset.id) AS count532 FROM asset533 ${queryFilterJoinClause}534 ${queryFilterWhereClause}535 AND third_level_subject.code IS NOT NULL536 GROUP BY537 third_level_subject.code538 ORDER BY539 third_level_subject.name ASC,540 third_level_subject.code ASC541 `,542 queryFilterValues543 )544 ).rows;545 const fourLevelSubjects = (546 await ctx.appDbQuery(547 `548 SELECT549 fourth_level_subject.code AS id,550 fourth_level_subject.name AS title,551 COUNT(DISTINCT asset.id) AS count552 FROM asset553 ${queryFilterJoinClause}554 ${queryFilterWhereClause}555 AND fourth_level_subject.code IS NOT NULL556 GROUP BY557 fourth_level_subject.code558 ORDER BY559 fourth_level_subject.name ASC,560 fourth_level_subject.code ASC561 `,562 queryFilterValues563 )564 ).rows;565 subjectsThirdLevelData = thirdLevelSubjects.map((row) => ({566 id: row.id,567 title: row.title,568 count: row.count,569 child_subjects: fourLevelSubjects570 .filter((child) => child.id.substring(0, 3) === row.id)571 .map((child_subject) => ({572 id: child_subject.id,573 title: child_subject.title,574 count: child_subject.count,575 })),576 }));577 }578 //Get other filters data579 {580 const resultLevel = (581 await ctx.appDbQuery(582 `583 SELECT584 af_level.level AS value,585 COUNT(DISTINCT asset.id) AS count586 FROM587 asset588 ${queryFilterJoinClause}589 ${queryFilterWhereClause}590 AND af_level.level IS NOT NULL591 GROUP BY592 af_level.level593 ORDER BY594 af_level.level ASC595 `,596 queryFilterValues597 )598 ).rows;599 const resultScottishLevel = (600 await ctx.appDbQuery(601 `602 SELECT603 af_scottish_level.scottish_level AS value,604 COUNT(DISTINCT asset.id) AS count605 FROM606 asset607 ${queryFilterJoinClause}608 ${queryFilterWhereClause}609 AND af_scottish_level.scottish_level IS NOT NULL610 GROUP BY611 af_scottish_level.scottish_level612 ORDER BY613 af_scottish_level.scottish_level ASC614 `,615 queryFilterValues616 )617 ).rows;618 const resultCollection = (619 await ctx.appDbQuery(620 `621 SELECT622 af_collection.collection AS value,623 COUNT(DISTINCT asset.id) AS count624 FROM625 asset626 ${queryFilterJoinClause}627 ${queryFilterWhereClause}628 AND af_collection.collection IS NOT NULL629 GROUP BY630 af_collection.collection631 ORDER BY632 af_collection.collection ASC633 `,634 queryFilterValues635 )636 ).rows;637 const resultExam = (638 await ctx.appDbQuery(639 `640 SELECT641 af_exam.exam AS value,642 COUNT(DISTINCT asset.id) AS count643 FROM644 asset645 ${queryFilterJoinClause}646 ${queryFilterWhereClause}647 AND af_exam.exam IS NOT NULL648 GROUP BY649 af_exam.exam650 ORDER BY651 af_exam.exam ASC652 `,653 queryFilterValues654 )655 ).rows;656 const resultContentForm = (657 await ctx.appDbQuery(658 `659 SELECT660 content_form AS value,661 COUNT(DISTINCT asset.id) AS count662 FROM663 asset664 ${queryFilterJoinClause}665 ${queryFilterWhereClause}666 AND content_form IS NOT NULL667 GROUP BY668 content_form669 ORDER BY670 content_form ASC671 `,672 queryFilterValues673 )674 ).rows;675 const resultExamBoard = (676 await ctx.appDbQuery(677 `678 SELECT679 af_exam_board.exam_board AS value,680 COUNT(DISTINCT asset.id) AS count681 FROM682 asset683 ${queryFilterJoinClause}684 ${queryFilterWhereClause}685 AND af_exam_board.exam_board IS NOT NULL686 GROUP BY687 af_exam_board.exam_board688 ORDER BY689 af_exam_board.exam_board ASC690 `,691 queryFilterValues692 )693 ).rows;694 const resultKeyStage = (695 await ctx.appDbQuery(696 `697 SELECT698 af_key_stage.key_stage AS value,699 COUNT(DISTINCT asset.id) AS count700 FROM701 asset702 ${queryFilterJoinClause}703 ${queryFilterWhereClause}704 AND af_key_stage.key_stage IS NOT NULL705 GROUP BY706 af_key_stage.key_stage707 ORDER BY708 af_key_stage.key_stage ASC709 `,710 queryFilterValues711 )712 ).rows;713 const resultPublishers = (714 await ctx.appDbQuery(715 `716 SELECT717 asset.publisher_name_log AS value,718 COUNT(DISTINCT asset.id) AS count719 FROM720 asset721 ${queryFilterJoinClause}722 ${queryFilterWhereClause}723 AND asset.publisher_id IS NOT NULL724 GROUP BY725 asset.publisher_name_log726 ORDER BY727 asset.publisher_name_log ASC728 `,729 queryFilterValues730 )731 ).rows;732 const resultLanguages = (733 await ctx.appDbQuery(734 `735 SELECT736 af_language.language AS value,737 COUNT(DISTINCT asset.id) AS count738 FROM739 asset740 ${queryFilterJoinClause}741 ${queryFilterWhereClause}742 AND af_language.language IS NOT NULL743 GROUP BY744 af_language.language745 ORDER BY746 af_language.language ASC747 `,748 queryFilterValues749 )750 ).rows;751 resultFilter.push({752 id: "format",753 title: `Format`,754 data: resultContentForm.map((item) => ({ id: item.value, title: getAssetFormatTitle(item.value), count: item.count })),755 });756 resultFilter.push({757 id: "language",758 title: "Language",759 data: resultLanguages.map((item) => ({ id: item.value, title: getNiceLanguage(item.value), count: item.count })),760 });761 resultFilter.push({762 id: "level",763 title: `Level`,764 data: resultLevel.map((item) => ({ id: item.value, title: item.value, count: item.count })),765 });766 if (subjectsThirdLevelData.length > 0) {767 resultFilter.push({768 id: "subject",769 title: "Subject",770 data: subjectsThirdLevelData,771 });772 }773 resultFilter.push({774 id: "exam",775 title: `Exam`,776 data: resultExam.map((item) => ({ id: item.value, title: item.value, count: item.count })),777 });778 resultFilter.push({779 id: "exam_board",780 title: `Exam Board`,781 data: resultExamBoard.map((item) => ({ id: item.value, title: item.value, count: item.count })),782 });783 resultFilter.push({784 id: "scottish_level",785 title: `Scottish Class/Stage`,786 data: resultScottishLevel.map((item) => ({ id: item.value, title: item.value, count: item.count })),787 });788 resultFilter.push({789 id: "key_stage",790 title: `Key Stage`,791 data: resultKeyStage.map((item) => ({ id: item.value, title: item.value, count: item.count })),792 });793 resultFilter.push({794 id: "collection",795 title: `Collection`,796 data: resultCollection.map((item) => ({ id: item.value, title: item.value, count: item.count })),797 });798 resultFilter.push({799 id: "publisher",800 title: `Publisher`,801 data: resultPublishers.map((item) => ({ id: item.value, title: item.value, count: item.count })),802 });803 }804 return {805 unfiltered_count: unfilteredCount,806 results: results,807 resultFilter: resultFilter,808 resultFilterCount: resultFilterCount,809 };810 } catch (e) {811 ctx.throw(400, e.message);812 }...
globals.ts
Source:globals.ts
...91 } catch (err) {92 return undefined;93 }94}95function extractJoin(instance: unknown[]) {96 try {97 return instance.join;98 } catch (err) {99 return undefined;100 }101}102function extractMap(instance: unknown[]) {103 try {104 return instance.map;105 } catch (err) {106 return undefined;107 }108}109function extractFilter(instance: unknown[]) {110 try {111 return instance.filter;112 } catch (err) {113 return undefined;114 }115}116function extractPush(instance: unknown[]) {117 try {118 return instance.push;119 } catch (err) {120 return undefined;121 }122}123function extractPop(instance: unknown[]) {124 try {125 return instance.pop;126 } catch (err) {127 return undefined;128 }129}130function extractSplice(instance: unknown[]) {131 try {132 return instance.splice;133 } catch (err) {134 return undefined;135 }136}137function extractSlice(instance: unknown[]) {138 try {139 return instance.slice;140 } catch (err) {141 return undefined;142 }143}144function extractSort(instance: unknown[]) {145 try {146 return instance.sort;147 } catch (err) {148 return undefined;149 }150}151export function safeForEach<T>(instance: T[], fn: (value: T, index: number, array: T[]) => void): void {152 if (extractForEach(instance) === untouchedForEach) {153 return instance.forEach(fn);154 }155 return safeApply(untouchedForEach, instance, [fn]);156}157export function safeIndexOf<T>(158 instance: readonly T[],159 ...args: [searchElement: T, fromIndex?: number | undefined]160): number {161 if (extractIndexOf(instance) === untouchedIndexOf) {162 return instance.indexOf(...args);163 }164 return safeApply(untouchedIndexOf, instance, args);165}166export function safeJoin<T>(instance: T[], ...args: [separator?: string | undefined]): string {167 if (extractJoin(instance) === untouchedJoin) {168 return instance.join(...args);169 }170 return safeApply(untouchedJoin, instance, args);171}172export function safeMap<T, U>(instance: T[], fn: (value: T, index: number, array: T[]) => U): U[] {173 if (extractMap(instance) === untouchedMap) {174 return instance.map(fn);175 }176 return safeApply(untouchedMap, instance, [fn]);177}178export function safeFilter<T, U extends T>(179 instance: T[],180 predicate: ((value: T, index: number, array: T[]) => value is U) | ((value: T, index: number, array: T[]) => unknown)181): U[] {...
Using AI Code Generation
1"dependencies": {2 },3 "resolutions": {4 }5"dependencies": {6 },7 "resolutions": {8 }9"workspaces": {10 }11"workspaces": {
Using AI Code Generation
1var fastCheck = require('fast-check');2var fc = fastCheck.default;3var extractJoin = fastCheck.extractJoin;4var join = fastCheck.join;5var arb = fc.integer();6var arb2 = fc.integer();7var arb3 = fc.integer();8var arb4 = fc.integer();9var arb5 = fc.integer();10var arb6 = fc.integer();11var arb7 = fc.integer();12var arb8 = fc.integer();13var arb9 = fc.integer();14var arb10 = fc.integer();15var arb11 = fc.integer();16var arb12 = fc.integer();17var arb13 = fc.integer();18var arb14 = fc.integer();19var arb15 = fc.integer();20var arb16 = fc.integer();21var arb17 = fc.integer();22var arb18 = fc.integer();23var arb19 = fc.integer();24var arb20 = fc.integer();25var arb21 = fc.integer();26var arb22 = fc.integer();27var arb23 = fc.integer();28var arb24 = fc.integer();29var arb25 = fc.integer();30var arb26 = fc.integer();31var arb27 = fc.integer();32var arb28 = fc.integer();33var arb29 = fc.integer();34var arb30 = fc.integer();35var arb31 = fc.integer();36var arb32 = fc.integer();37var arb33 = fc.integer();38var arb34 = fc.integer();39var arb35 = fc.integer();40var arb36 = fc.integer();41var arb37 = fc.integer();42var arb38 = fc.integer();43var arb39 = fc.integer();44var arb40 = fc.integer();45var arb41 = fc.integer();46var arb42 = fc.integer();47var arb43 = fc.integer();48var arb44 = fc.integer();49var arb45 = fc.integer();50var arb46 = fc.integer();51var arb47 = fc.integer();52var arb48 = fc.integer();53var arb49 = fc.integer();54var arb50 = fc.integer();55var arb51 = fc.integer();56var arb52 = fc.integer();57var arb53 = fc.integer();58var arb54 = fc.integer();59var arb55 = fc.integer();60var arb56 = fc.integer();61var arb57 = fc.integer();62var arb58 = fc.integer();63var arb59 = fc.integer();64var arb60 = fc.integer();65var arb61 = fc.integer();66var arb62 = fc.integer();67var arb63 = fc.integer();68var arb64 = fc.integer();69var arb65 = fc.integer();70var arb66 = fc.integer();
Using AI Code Generation
1const { extractJoin } = require("fast-check-monorepo");2const { string } = require("fast-check");3const fc = require("fast-check");4const extractJoinArb = (arb) => extractJoin(arb, (a) => a.split(""), (a, b) => a + b);5const property = fc.property(extractJoinArb(string()), (s) => s === s.split("").join(""));6fc.assert(property);
Using AI Code Generation
1const {extractJoin} = require('fast-check-monorepo');2const {extractJoin} = require('fast-check-monorepo');3const {extractJoin} = require('fast-check-monorepo');4const {extractJoin} = require('fast-check-monorepo');5const {extractJoin} = require('fast-check-monorepo');6const {extractJoin} = require('fast-check-monorepo');7const {extractJoin} = require('fast-check-monorepo');8const {extractJoin} = require('fast-check-monorepo');
Using AI Code Generation
1var extractJoin = require('fast-check-monorepo').extractJoin;2var arr1 = [1, 2, 3, 4];3var arr2 = ['a', 'b', 'c', 'd'];4var arr3 = ['A', 'B', 'C', 'D'];5var arr4 = ['!', '@', '#', '$'];6console.log(extractJoin(arr1, arr2, arr3, arr4));7var extractJoin = require('fast-check-monorepo').extractJoin;8var arr1 = [1, 2, 3, 4];9var arr2 = ['a', 'b', 'c', 'd'];10var arr3 = ['A', 'B', 'C', 'D'];11var arr4 = ['!', '@', '#', '$'];12var arr5 = ['x', 'y', 'z', 'w'];13console.log(extractJoin(arr1, arr2, arr3, arr4, arr5));14var extractJoin = require('fast-check-monorepo').extractJoin;15var arr1 = [1, 2, 3, 4];16var arr2 = ['a', 'b', 'c', 'd'];17var arr3 = ['A', 'B', 'C', 'D'];18var arr4 = ['!', '@', '#', '$'];
Using AI Code Generation
1const { extractJoin } = require('fast-check-monorepo');2const str = "Hello World";3const arr = extractJoin(str);4console.log(arr);5const { extractJoin } = require('fast-check-monorepo');6const str = "Hello World";7const arr = extractJoin(str);8console.log(arr);9const { extractJoin } = require('fast-check-monorepo');10const str = "Hello World";11const arr = extractJoin(str);12console.log(arr);13const { extractJoin } = require('fast-check-monorepo');14const str = "Hello World";15const arr = extractJoin(str);16console.log(arr);17const { extractJoin } = require('fast-check-monorepo');18const str = "Hello World";19const arr = extractJoin(str);20console.log(arr);21const { extractJoin } = require('fast-check-monorepo');22const str = "Hello World";23const arr = extractJoin(str);24console.log(arr);25const { extractJoin } = require('fast-check-monorepo');26const str = "Hello World";27const arr = extractJoin(str);28console.log(arr);
Using AI Code Generation
1const fc = require('fast-check');2const { extractJoin } = require('fast-check-monorepo');3fc.assert(4 fc.property(fc.string(3, 100), (s) => {5 const firstTwo = s.substring(0, 2);6 const expected = s + firstTwo;7 const result = extractJoin(s);8 return result === expected;9 }),10);11const fc = require('fast-check');12const { extractJoin } = require('fast-check-monorepo');13fc.assert(14 fc.property(fc.string(3, 100), (s) => {15 const firstTwo = s.substring(0, 2);16 const expected = s + firstTwo;17 const result = extractJoin(s);18 return result === expected;19 }),20);21const fc = require('fast-check');22const { extractJoin } = require('fast-check-monorepo');23fc.assert(24 fc.property(fc.string(3, 100), (s) => {
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!!