How to use customSlices method in fast-check-monorepo

Best JavaScript code snippet using fast-check-monorepo

subset.js

Source:subset.js Github

copy

Full Screen

1/*! DSRKafuU (https://dsrkafuu.net) | Copyright (c) MIT License */2const path = require('path');3const crypto = require('crypto');4const fse = require('fs-extra');5const glob = require('glob');6const childProcess = require('child_process');7const config = fse.readJSONSync(path.resolve(__dirname, '../config.json'));8// output of `fetch.js` script9const ranges = fse.readJSONSync(path.resolve(__dirname, '../raw/ranges.json'));10/**11 * get all supported unicodes of a font file12 * @returns {Promise<Set<string>>}13 */14async function getSupportedUnicodeSet(file) {15 // get cmap ttx with fonttools16 const ttxFile = file.replace(/\.ttf$/, '.ttx');17 childProcess.execSync(`fonttools ttx -t cmap -o ${ttxFile} ${file}`);18 const cmap = fse.readFileSync(ttxFile, 'utf-8');19 // match unicodes20 const unicodeSet = new Set();21 const exp = /<map +code="([^"]+)"/gi;22 let expr = exp.exec(cmap);23 while (expr && expr[1]) {24 const str = expr[1];25 const unicode = str.toLowerCase().replace(/^0x/, 'U+');26 unicodeSet.add(unicode);27 expr = exp.exec(cmap);28 }29 // remove ttx file30 fse.unlinkSync(ttxFile);31 return unicodeSet;32}33/**34 * get hash of a font file35 * @param {string} file36 * @returns {string}37 */38function getHash(file) {39 const hasher = crypto.createHash('sha1');40 hasher.update(fse.readFileSync(file));41 return hasher.digest('hex');42}43/**44 * unicode (hex) to number45 * @param {string} u46 * @returns {number}47 */48function u2n(u) {49 return Number.parseInt(/U\+([0-9A-F]+)/i.exec(u)[1], 16);50}51/**52 * merge sequential unicodes for css53 * @param {string[]} unicodes54 * @returns {string}55 */56function mergeUnicodes(unicodes) {57 unicodes = [...unicodes].sort((a, b) => u2n(a) - u2n(b));58 const merged = [];59 let start = null;60 let end = null;61 for (const cur of unicodes) {62 if (start === null) {63 start = cur;64 end = cur;65 } else if (u2n(end) === u2n(cur) - 1) {66 end = cur;67 } else {68 merged.push(start === end ? start : `${start}-${u2n(end).toString(16)}`);69 start = cur;70 end = cur;71 }72 }73 if (start !== null) {74 merged.push(start === end ? start : `${start}-${u2n(end).toString(16)}`);75 }76 return merged.join(',');77}78/**79 * @param {Set<string>} set80 */81async function sliceUnprocessedUnicodes(set) {82 if (set.size === 0) {83 return {};84 }85 const arr = Array.from(set).sort((a, b) => u2n(a) - u2n(b));86 // 200 char per slice87 const slices = {};88 let idx = 1;89 slices[`[-${idx}]`] = [];90 for (const u of arr) {91 if (slices[`[-${idx}]`].length >= 200) {92 idx++;93 slices[`[-${idx}]`] = [];94 } else {95 slices[`[-${idx}]`].push(u);96 }97 }98 // if last slice is to small99 if (slices[`[-${idx}]`].length < 100) {100 slices[`[-${idx - 1}]`] = [101 ...slices[`[-${idx - 1}]`],102 ...slices[`[-${idx}]`],103 ];104 delete slices[`[-${idx}]`];105 }106 return slices;107}108let empty = false;109/**110 * create subsets for a font file111 * @param {string} file112 */113async function createSubsets(file) {114 // get all supported unicodes115 const supportedUnicodeSet = await getSupportedUnicodeSet(file);116 // filter the ranges117 const rangesOfThisFile = { ...ranges };118 const processedUnicodeSet = new Set();119 Object.entries(rangesOfThisFile).forEach(([key, unicodes]) => {120 // filter the unicodes121 const supportedUnicodes = unicodes.filter((unicode) =>122 supportedUnicodeSet.has(unicode)123 );124 // if no unicode is supported, delete the range125 if (supportedUnicodes.length === 0) {126 delete rangesOfThisFile[key];127 } // if some unicode is supported, update the range128 else {129 rangesOfThisFile[key] = supportedUnicodes;130 // mark supported unicodes as processed131 supportedUnicodes.forEach((unicode) => processedUnicodeSet.add(unicode));132 }133 });134 // mark those unicodes supported in font but not processed135 const unProcessedUnicodeSet = new Set();136 Array.from(supportedUnicodeSet).forEach((unicode) => {137 if (!processedUnicodeSet.has(unicode)) {138 unProcessedUnicodeSet.add(unicode);139 }140 });141 // write the result to json142 fse.writeJSONSync(143 path.resolve(__dirname, '../raw/ranges-supported.json'),144 rangesOfThisFile,145 { spaces: 2 }146 );147 fse.writeJSONSync(148 path.resolve(__dirname, '../raw/ranges-unprocessed.json'),149 Array.from(unProcessedUnicodeSet),150 { spaces: 2 }151 );152 // dont need extend slices153 if (!config.extendSlices) {154 unProcessedUnicodeSet.clear();155 }156 // has custom unicodes157 if (config.customUnicodes && config.customUnicodes.length > 0) {158 config.customUnicodes.forEach((unicode) => {159 unProcessedUnicodeSet.add(unicode);160 });161 }162 const customSlices = await sliceUnprocessedUnicodes(unProcessedUnicodeSet);163 fse.writeJSONSync(164 path.resolve(__dirname, '../raw/ranges-custom.json'),165 customSlices,166 { spaces: 2 }167 );168 const hash = getHash(file).substring(0, 8);169 const outFile = file.replace(/\.ttf$/, `.subset.ttf`);170 const baseName = path.basename(file, '.ttf');171 const targetFolder = path.resolve(__dirname, '../raw/subset');172 const fontWeight = baseName.split('-')[1];173 let css = '';174 fse.ensureDirSync(targetFolder);175 if (!empty) {176 fse.emptyDirSync(targetFolder);177 empty = true;178 }179 // create subsets180 console.log(`Creating subsets for "${file}"...`);181 Object.entries({ ...rangesOfThisFile, ...customSlices }).forEach(182 ([key, unicodes]) => {183 const _unicodes = unicodes.join(',');184 childProcess.execSync(185 `fonttools subset --unicodes="${_unicodes}" ${file}`186 );187 // move subset to the output directory188 const index = /\[(-?[0-9]+)\]/i.exec(key)[1];189 const targetFile = path.resolve(190 targetFolder,191 `${baseName}.${hash}.${index}.ttf`192 );193 fse.moveSync(outFile, targetFile);194 childProcess.execSync(`fonttools ttLib.woff2 compress ${targetFile}`);195 fse.unlinkSync(targetFile);196 css +=197 `/*${key}*/` +198 `@font-face{font-family:MiSans;font-style:normal;` +199 `font-weight:${fontWeight};font-display:swap;` +200 `src: url('${baseName}.${hash}.${index}.woff2') format('woff2');` +201 `unicode-range:${mergeUnicodes(unicodes)};}\n`;202 }203 );204 fse.writeFileSync(205 path.resolve(targetFolder, `${baseName}.min.css`),206 css.trim(),207 'utf-8'208 );209 console.log(`Done for ${Object.keys(rangesOfThisFile).length} subsets`);210}211async function main() {212 const files = glob213 .sync('raw/full/*.ttf')214 .map((file) => path.resolve(__dirname, '../', file));215 for (const file of files) {216 await createSubsets(file);217 }218}219main().catch((e) => {220 console.log(e);221 process.exit(1);...

Full Screen

Full Screen

medication.js

Source:medication.js Github

copy

Full Screen

1/* eslint-disable react/prop-types */2import React from 'react';3import { fetchSingle, fetchFAQs } from 'client/prismic';4import Page from 'components/Page';5import { SliceZone } from 'components/SliceZone';6import { PricingFAQs } from 'components/Pricing';7function Medication({ data, meta }) {8 const customSlices = [9 {10 type: 'pricing_faqs',11 Component: PricingFAQs,12 },13 ];14 return (15 <SliceZone slices={data.body} meta={meta} custom={customSlices} />16 );17}18export async function getStaticProps({ preview = false, previewData }) {19 const page = await fetchSingle('medication', { ...previewData });20 const slicesWithFAQData = await fetchFAQs(page.data.body);21 page.data.body = slicesWithFAQData;22 // pathname of the page for canonical tag23 page.meta.pathname = 'medication';24 return {25 props: {26 ...page,27 preview,28 },29 };30}31Medication.layout = Page;...

Full Screen

Full Screen

index.js

Source:index.js Github

copy

Full Screen

1import React from 'react';2import PropTypes from 'types';3import { fetchSingle } from 'client/prismic';4import Page from 'components/Page';5import { SliceZone } from 'components/SliceZone';6import { FAQSection } from 'components/FAQs';7function FAQs({ data }) {8 const customSlices = [{9 type: 'faq_section',10 Component: FAQSection,11 }];12 return <SliceZone slices={data.body} custom={customSlices} />;13}14FAQs.propTypes = {15 data: PropTypes.any,16};17export async function getStaticProps({ preview = false, previewData }) {18 const page = await fetchSingle('faqs_page', {19 fetchLinks: ['faq.title'],20 ...previewData,21 });22 // pathname of the page for canonical tag23 page.meta.pathname = 'faqs';24 return {25 props: {26 ...page,27 preview,28 },29 };30}31FAQs.layout = Page;...

Full Screen

Full Screen

Using AI Code Generation

copy

Full Screen

1const fc = require('fast-check');2const { customSlices } = require('fast-check/lib/arbitrary/_internals/SlicesArbitrary');3const { integer } = require('fast-check/lib/arbitrary/IntegerArbitrary');4const { array } = require('fast-check/lib/arbitrary/ArrayArbitrary');5const { tuple } = require('fast-check/lib/arbitrary/TupleArbitrary');6const mySlicesArb = customSlices(array(integer()), tuple(integer(), integer()));7fc.assert(8 fc.property(mySlicesArb, (slices) => {9 console.log(slices);10 return true;11 })12);13const fc = require('fast-check');14const { customSlices } = require('fast-check/lib/arbitrary/_internals/SlicesArbitrary');15const { integer } = require('fast-check/lib/arbitrary/IntegerArbitrary');16const { array } = require('fast-check/lib/arbitrary/ArrayArbitrary');17const { tuple } = require('fast-check/lib/arbitrary/TupleArbitrary');18const mySlicesArb = customSlices(array(integer()), tuple(integer(), integer()));19fc.assert(20 fc.property(mySlicesArb, (slices) => {21 console.log(slices);22 return true;23 })24);25const fc = require('fast-check');26const { customSlices } = require('fast-check/lib/arbitrary/_internals/SlicesArbitrary');27const { integer } = require('fast-check/lib/arbitrary/IntegerArbitrary');28const { array } = require('fast-check/lib/arbitrary/ArrayArbitrary');29const { tuple } = require('fast-check/lib/arbitrary/TupleArbitrary');30const mySlicesArb = customSlices(array(integer()), tuple(integer(), integer()));31fc.assert(32 fc.property(mySlicesArb, (slices) => {33 console.log(slices);34 return true;35 })36);37const fc = require('fast-check');38const { customSlices } = require('fast-check/lib/arbitrary/_internals/SlicesArbitrary');39const { integer } = require('fast-check/lib/arbitrary

Full Screen

Using AI Code Generation

copy

Full Screen

1const { customSlices } = require('fast-check');2const { generate } = require('fast-check/lib/check/arbitrary/ArrayArbitrary');3const { tuple } = require('fast-check/lib/check/arbitrary/TupleArbitrary');4const { nat } = require('fast-check/lib/check/arbitrary/NatArbitrary');5const { string } = require('fast-check/lib/check/arbitrary/StringArbitrary');6const myCustomSlices = customSlices(7 generate(tuple(nat(), string())),8 (x) => x[0]9);10myCustomSlices(0, 10).forEach((x) => console.log(x));11const { customSlices } = require('fast-check');12const { generate } = require('fast-check/lib/check/arbitrary/ArrayArbitrary');13const { tuple } = require('fast-check/lib/check/arbitrary/TupleArbitrary');14const { nat } = require('fast-check/lib/check/arbitrary/NatArbitrary');15const { string } = require('fast-check/lib/check/arbitrary/StringArbitrary');16const myCustomSlices = customSlices(17 generate(tuple(nat(), string())),18 (x) => x[0]19);20myCustomSlices(0, 10).forEach((x) => console.log(x));

Full Screen

Using AI Code Generation

copy

Full Screen

1const fc = require('fast-check');2fc.customSlices([1,2,3,4,5,6,7,8,9,10], 3, 3).forEach(function(item) {3 console.log(item);4});5const fc = require('fast-check');6fc.customSlices([1,2,3,4,5,6,7,8,9,10], 3, 4).forEach(function(item) {7 console.log(item);8});9const fc = require('fast-check');10fc.customSlices([1,2,3,4,5,6,7,8,9,10], 3, 5).forEach(function(item) {11 console.log(item);12});13const fc = require('fast-check');14fc.customSlices([1,2,3,4,5,6,7,8,9,10], 3, 6).forEach(function(item) {15 console.log(item);16});

Full Screen

Using AI Code Generation

copy

Full Screen

1const fc = require('fast-check');2const { customSlices } = require('fast-check/lib/arbitrary/customSlices.js');3const { range } = require('fast-check/lib/arbitrary/range.js');4const a = customSlices(5 range(0, 100),6 (s) => s.length === 2,7 (s) => s[0] < s[1],8 (s) => s[0] + s[1]9);10fc.assert(11 fc.property(a, (a) => {12 console.log(a);13 return true;14 })15);16const fc = require('fast-check');17const { customSlices } = require('fast-check/lib/arbitrary/customSlices.js');18const { range } = require('fast-check/lib/arbitrary/range.js');19const a = customSlices(20 range(0, 100),21 (s) => s.length === 2,22 (s) => s[0] < s[1],23 (s) => s[0] + s[1]24);25fc.assert(26 fc.property(a, (a) => {27 console.log(a);28 return true;29 })30);31const fc = require('fast-check');32const { customSlices } = require('fast-check/lib/arbitrary/customSlices.js');33const { range } = require('fast-check/lib/arbitrary/range.js');34const a = customSlices(35 range(0, 100),36 (s) => s.length === 2,37 (s) => s[0] < s[1],38 (s) => s[0] + s[1]39);40fc.assert(41 fc.property(a, (a) => {42 console.log(a);43 return true;44 })45);46const fc = require('fast-check');47const { customSlices } = require('fast-check/lib/arbitrary/customSlices.js');48const { range } = require('fast-check/lib/arbitrary/range.js');49const a = customSlices(50 range(0, 100),51 (s) =>

Full Screen

Using AI Code Generation

copy

Full Screen

1const fc = require("fast-check");2const { customSlices } = require("fast-check/lib/arbitrary/_internals/CustomSlicesArbitrary");3const arb = customSlices(4 fc.integer(),5 (s) => s.length < 5,6 (s) => s.reduce((acc, cur) => acc + cur, 0) < 10,7 (s) => s.reduce((acc, cur) => acc * cur, 1) < 1008);9fc.assert(10 fc.property(arb, (s) => {11 console.log(s);12 return true;13 })14);15const fc = require("fast-check");16const { customSlices } = require("fast-check/lib/arbitrary/_internals/CustomSlicesArbitrary");17const arb = customSlices(18 fc.integer(),19 (s) => s.length < 5,20 (s) => s.reduce((acc, cur) => acc + cur, 0) < 10,21 (s) => s.reduce((acc, cur) => acc * cur, 1) < 10022);23fc.assert(24 fc.property(arb, (s) => {25 console.log(s);26 return true;27 })28);29const fc = require("fast-check");30const { customSlices } = require("fast-check/lib/arbitrary/_internals/CustomSlicesArbitrary");31const arb = customSlices(32 fc.integer(),33 (s) => s.length < 5,34 (s) => s.reduce((acc, cur) => acc + cur, 0) < 10,35 (s) => s.reduce((acc, cur) => acc * cur, 1) < 10036);37fc.assert(38 fc.property(arb, (s) => {39 console.log(s);40 return true;41 })42);43const fc = require("fast-check");44const { customSlices } = require("fast-check/lib/arbitrary/_internals/CustomSlicesArbitrary");45const arb = customSlices(46 fc.integer(),47 (

Full Screen

Using AI Code Generation

copy

Full Screen

1const fc = require('fast-check');2const customSlices = require('fast-check-monorepo').customSlices;3const arbitraryOfSubArrays = customSlices(array);4fc.assert(5 fc.property(arbitraryOfSubArrays, (subArray) => {6 return subArray.length < 3;7 })8);

Full Screen

Automation Testing Tutorials

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.

LambdaTest Learning Hubs:

YouTube

You could also refer to video tutorials over LambdaTest YouTube channel to get step by step demonstration from industry experts.

Run fast-check-monorepo automation tests on LambdaTest cloud grid

Perform automation testing on 3000+ real desktop and mobile devices online.

Try LambdaTest Now !!

Get 100 minutes of automation test minutes FREE!!

Next-Gen App & Browser Testing Cloud

Was this article helpful?

Helpful

NotHelpful