Best JavaScript code snippet using wpt
fraction.ts
Source:fraction.ts
1import _Big, { RoundingMode } from 'big.js';2import _Decimal from 'decimal.js-light';3import JSBI from 'jsbi';4import invariant from 'tiny-invariant';5import toFormat from 'toformat';6import { BigintIsh, ONE, Rounding } from '../constants/constants';7import { parseBigintIsh } from '../utils/parse-bigint-ish';8const Decimal = toFormat(_Decimal);9const Big = toFormat(_Big);10const toSignificantRounding = {11 [Rounding.ROUND_DOWN]: Decimal.ROUND_DOWN,12 [Rounding.ROUND_HALF_UP]: Decimal.ROUND_HALF_UP,13 [Rounding.ROUND_UP]: Decimal.ROUND_UP,14};15const toFixedRounding = {16 [Rounding.ROUND_DOWN]: RoundingMode.RoundDown,17 [Rounding.ROUND_HALF_UP]: RoundingMode.RoundHalfUp,18 [Rounding.ROUND_UP]: RoundingMode.RoundUp,19};20export class Fraction {21 public readonly numerator: JSBI;22 public readonly denominator: JSBI;23 public constructor(numerator: BigintIsh, denominator: BigintIsh = ONE) {24 this.numerator = parseBigintIsh(numerator);25 this.denominator = parseBigintIsh(denominator);26 }27 /**28 * Performs floor division29 */30 public get quotient(): JSBI {31 return JSBI.divide(this.numerator, this.denominator);32 }33 /**34 * Remainder after floor division35 */36 public get remainder(): Fraction {37 return new Fraction(JSBI.remainder(this.numerator, this.denominator), this.denominator);38 }39 public invert(): Fraction {40 return new Fraction(this.denominator, this.numerator);41 }42 public add(other: Fraction | BigintIsh): Fraction {43 const otherParsed = other instanceof Fraction ? other : new Fraction(parseBigintIsh(other));44 if (JSBI.equal(this.denominator, otherParsed.denominator)) {45 return new Fraction(JSBI.add(this.numerator, otherParsed.numerator), this.denominator);46 }47 return new Fraction(48 JSBI.add(JSBI.multiply(this.numerator, otherParsed.denominator), JSBI.multiply(otherParsed.numerator, this.denominator)),49 JSBI.multiply(this.denominator, otherParsed.denominator),50 );51 }52 public subtract(other: Fraction | BigintIsh): Fraction {53 const otherParsed = other instanceof Fraction ? other : new Fraction(parseBigintIsh(other));54 if (JSBI.equal(this.denominator, otherParsed.denominator)) {55 return new Fraction(JSBI.subtract(this.numerator, otherParsed.numerator), this.denominator);56 }57 return new Fraction(58 JSBI.subtract(JSBI.multiply(this.numerator, otherParsed.denominator), JSBI.multiply(otherParsed.numerator, this.denominator)),59 JSBI.multiply(this.denominator, otherParsed.denominator),60 );61 }62 public lessThan(other: Fraction | BigintIsh): boolean {63 const otherParsed = other instanceof Fraction ? other : new Fraction(parseBigintIsh(other));64 return JSBI.lessThan(JSBI.multiply(this.numerator, otherParsed.denominator), JSBI.multiply(otherParsed.numerator, this.denominator));65 }66 public equalTo(other: Fraction | BigintIsh): boolean {67 const otherParsed = other instanceof Fraction ? other : new Fraction(parseBigintIsh(other));68 return JSBI.equal(JSBI.multiply(this.numerator, otherParsed.denominator), JSBI.multiply(otherParsed.numerator, this.denominator));69 }70 public greaterThan(other: Fraction | BigintIsh): boolean {71 const otherParsed = other instanceof Fraction ? other : new Fraction(parseBigintIsh(other));72 return JSBI.greaterThan(JSBI.multiply(this.numerator, otherParsed.denominator), JSBI.multiply(otherParsed.numerator, this.denominator));73 }74 public multiply(other: Fraction | BigintIsh): Fraction {75 const otherParsed = other instanceof Fraction ? other : new Fraction(parseBigintIsh(other));76 return new Fraction(JSBI.multiply(this.numerator, otherParsed.numerator), JSBI.multiply(this.denominator, otherParsed.denominator));77 }78 public divide(other: Fraction | BigintIsh): Fraction {79 const otherParsed = other instanceof Fraction ? other : new Fraction(parseBigintIsh(other));80 return new Fraction(JSBI.multiply(this.numerator, otherParsed.denominator), JSBI.multiply(this.denominator, otherParsed.numerator));81 }82 public toSignificant(significantDigits: number, format: object = { groupSeparator: '' }, rounding: Rounding = Rounding.ROUND_HALF_UP): string {83 invariant(Number.isInteger(significantDigits), `${significantDigits} is not an integer.`);84 invariant(significantDigits > 0, `${significantDigits} is not positive.`);85 Decimal.set({ precision: significantDigits + 1, rounding: toSignificantRounding[rounding] });86 const quotient = new Decimal(this.numerator.toString()).div(this.denominator.toString()).toSignificantDigits(significantDigits);87 return quotient.toFormat(quotient.decimalPlaces(), format);88 }89 public toFixed(decimalPlaces: number, format: object = { groupSeparator: '' }, rounding: Rounding = Rounding.ROUND_HALF_UP): string {90 invariant(Number.isInteger(decimalPlaces), `${decimalPlaces} is not an integer.`);91 invariant(decimalPlaces >= 0, `${decimalPlaces} is negative.`);92 Big.DP = decimalPlaces;93 Big.RM = toFixedRounding[rounding];94 return new Big(this.numerator.toString()).div(this.denominator.toString()).toFormat(decimalPlaces, format);95 }...
Fraction.js
Source:Fraction.js
1function Fraction(_numerator, _denominator) {2 if(_denominator == 0)3 throw Error('divide by zero');4 if(typeof _numerator == "number")5 this.numerator = _numerator;6 else this.numerator = 0;7 if(typeof _denominator == "number")8 this.denominator = _denominator;9 else this.denominator = 1;10 this.normalize();11}12/**13 * operator '+' *14 */15Fraction.addition = function(a, b) {16 return new Fraction(17 a.numerator * b.denominator + b.numerator * a.denominator,18 a.denominator * b.denominator19 );20};21/**22 * operator '-' *23 */24Fraction.subtraction = function(a, b) {25 return new Fraction(26 a.numerator * b.denominator - b.numerator * a.denominator,27 a.denominator * b.denominator28 );29};30/**31 * operator 'x' *32 */33Fraction.multiplication = function(a, b) {34 return new Fraction(35 a.numerator * b.numerator,36 a.denominator * b.denominator37 );38};39/**40 * operator ':' *41 */42Fraction.division = function(a, b) {43 return new Fraction(44 a.numerator * b.denominator,45 a.denominator * b.numerator46 );47};48/**49 * operator '==' *50 */51Fraction.isEqual = function(a, b) {52 if (typeof a.numerator == "undefined" ||53 typeof a.denominator == "undefined" ||54 typeof b != "number" &&55 (typeof b.numerator == "undefined" || typeof b.denominator == "undefined")) {56 throw new Error("incorrect input data");57 }58 if (typeof b == "number")59 if (a.numerator / a.denominator == b)60 return true;61 else return false;62 if (a.numerator / a.denominator == b.numerator / b.denominator)63 return true;64 return false;65};66/**67 * operator '!=' *68 */69Fraction.isNotEqual = function(a, b) {70 return !Fraction.isEqual(a, b);71};72/**73 * operator '>' *74 */75Fraction.more = function(a, b) {76 if (typeof a.numerator == "undefined" ||77 typeof a.denominator == "undefined" ||78 typeof b != "number" &&79 (typeof b.numerator == "undefined" || typeof b.denominator == "undefined"))80 throw new Error("incorrect input data");81 if (typeof b == "number")82 if (a.numerator / a.denominator > b)83 return true;84 else return false;85 if (a.numerator / a.denominator > b.numerator / b.denominator)86 return true;87 return false;88};89/**90 * operator '<' *91 */92Fraction.less = function(a, b) {93 if(Fraction.isEqual(a, b)) return false;94 return !Fraction.more(a, b);95};96/**97 * operator '>=' *98 */99Fraction.moreOrEqual = function(a, b) {100 return !Fraction.less(a, b);101};102/**103 * operator '<=' *104 */105Fraction.lessOrEqual = function(a, b) {106 return !Fraction.more(a, b);107};108Fraction.parse = function(str) {109 if(str=="") return new Fraction();110 var strs = str.split('/');111 var a = 0;112 var b = 1;113 a = parseInt(strs[0]);114 if (strs.length == 2)115 b = parseInt(strs[1]);116 if (typeof a != 'number' || isNaN(a) || typeof b != 'number' || isNaN(b) || strs.length > 2)117 throw Error("Ðеможливо зÑиÑаÑи данÑ");118 else return new Fraction(a, b);119};120Fraction.prototype.integerPart = function(){121 return parseInt(this.numerator/this.denominator);122};123Fraction.prototype.fractionalPart = function() {124 var t = this.numerator % this.denominator;125 return new Fraction(t >= 0 ? t : this.denominator + t, this.denominator);126};127Fraction.prototype.module = function(){128}129Fraction.prototype.isNull = function() {130 if(this.numerator == 0) return true;131 return false;132};133Fraction.prototype.toString = function() {134 return this.numerator + (this.denominator == 1 ? "" : "/" + this.denominator);135};136Fraction.prototype.contraction = function() {137 if(this.numerator == -0) this.numerator = 0;138 if (this.numerator == 0) {139 this.denominator = 1;140 return this;141 }142 var _gcd = gcd(Math.abs(this.numerator), Math.abs(this.denominator));143 this.numerator /= _gcd;144 this.denominator /= _gcd;145 return this;146};147Fraction.prototype.normalize = function() {148 if (this.denominator < 0) {149 this.numerator *= -1;150 this.denominator *= -1;151 }152 this.contraction();153};154gcd = function(a, b) {155 while (a != b)156 {157 if (a > b) a -= b;158 else b -= a;159 }160 return a;...
Using AI Code Generation
1var wptools = require('wptools');2var wiki = new wptools('Albert Einstein');3wiki.getDenominator(function(err, denominator) {4 console.log(denominator);5});6var wptools = require('wptools');7var wiki = new wptools('Albert Einstein');8wiki.getDenominator(function(err, denominator) {9 console.log(denominator);10});11var wptools = require('wptools');12var wiki = new wptools('Albert Einstein');13wiki.getDenominator(function(err, denominator) {14 console.log(denominator);15});16var wptools = require('wptools');17var wiki = new wptools('Albert Einstein');18wiki.getDenominator(function(err, denominator) {19 console.log(denominator);20});21var wptools = require('wptools');22var wiki = new wptools('Albert Einstein');23wiki.getDenominator(function(err, denominator) {24 console.log(denominator);25});26var wptools = require('wptools');27var wiki = new wptools('Albert Einstein');28wiki.getDenominator(function(err, denominator) {29 console.log(denominator);30});31var wptools = require('wptools');32var wiki = new wptools('Albert Einstein');33wiki.getDenominator(function(err, denominator) {34 console.log(denominator);35});36var wptools = require('wptools');37var wiki = new wptools('Albert Einstein');38wiki.getDenominator(function(err, denominator) {39 console.log(denominator);40});41var wptools = require('wptools');42var wiki = new wptools('Albert Einstein');43wiki.getDenominator(function(err, denominator) {44 console.log(denominator);45});
Using AI Code Generation
1var wptools = require('wptools');2var wiki = wptools.page('Albert Einstein');3wiki.get(function(err, resp) {4 console.log(resp);5});6var wptools = require('wptools');7var wiki = wptools.page('Albert Einstein');8wiki.get(function(err, resp) {9 console.log(resp);10});11var wptools = require('wptools');12var wiki = wptools.page('Albert Einstein');13wiki.get(function(err, resp) {14 console.log(resp);15});16var wptools = require('wptools');17var wiki = wptools.page('Albert Einstein');18wiki.get(function(err, resp) {19 console.log(resp);20});21var wptools = require('wptools');22var wiki = wptools.page('Albert Einstein');23wiki.get(function(err, resp) {24 console.log(resp);25});26var wptools = require('wptools');27var wiki = wptools.page('Albert Einstein');28wiki.get(function(err, resp) {29 console.log(resp);30});31var wptools = require('wptools');32var wiki = wptools.page('Albert Einstein');33wiki.get(function(err, resp) {34 console.log(resp);35});36var wptools = require('wptools');37var wiki = wptools.page('Albert Einstein');38wiki.get(function(err, resp) {39 console.log(resp);40});41var wptools = require('wptools');42var wiki = wptools.page('Albert Einstein');43wiki.get(function(err, resp) {44 console.log(resp);45});46var wptools = require('wptools');
Using AI Code Generation
1var wptools = require('wptools');2var fs = require('fs');3var page = wptools.page('Canada');4page.getDenominator(function(err, denominator) {5 console.log(denominator);6 fs.writeFileSync('denominator.json', JSON.stringify(denominator));7});8{9 "Canada": {10 "extract": "Canada (/kəˈnædə/ ( listen) kə-NAD-ə or /kæˈnɑːdə/ ( listen) kə-NAH-də; French: [kanada] ( listen)), officially the \"Dominion of Canada\", is a country in the northern part of North America. Its ten provinces and three territories extend from the Atlantic to the Pacific and northward into the Arctic Ocean, covering 9.98 million square kilometres (3.85 million square miles), making it the world's second-largest country by total area. Its southern and western border with the United States, stretching 8,891 kilometres (5,525 mi), is the world's longest bi-national land border. Canada's capital is Ottawa, and its three largest metropolitan areas are Toronto, Montreal, and Vancouver.",11 "coordinates": {12 },13 "pageprops": {14 },15 "pageterms": {
Using AI Code Generation
1var wptools = require('wptools');2var fs = require('fs');3var wtf = require("wtf_wikipedia");4var async = require('async');5var writeStream = fs.createWriteStream('output.json', {'flags': 'a'});6var pages = fs.readFileSync('pages.txt', 'utf8').split("7");8async.eachSeries(pages, function(page, callback) {9 wptools.denominator(page).then(function(data) {10 var infobox = data.infobox;11 var text = data.text;12 var wiki = wtf(text);13 var paragraphs = wiki.paragraphs();14 var categories = wiki.categories();15 var links = wiki.links();16 var summary = wiki.summary();17 var json = {18 };19 writeStream.write(JSON.stringify(json) + "20");21 callback();22 });23}, function(err) {24 console.log("Done");25});
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!!