Best JavaScript code snippet using storybook-root
validation.service.ts
Source:validation.service.ts
1import {Injectable} from '@angular/core';2import {AbstractControl, FormGroup, ValidatorFn} from '@angular/forms';3@Injectable({4 providedIn: 'root'5})6export class ValidationService {7 constructor() {8 }9 static blankSpaceValidator(reg: string | null = null): ValidatorFn {10 const match: RegExp = reg == null11 ? /^([^\s])([\s[a-zA-Z\u4E00-\u9FFF\u3400-\u4DFF\uF900-\uFAFF0-9_@./#&+-]*)([^\s])$/12 : new RegExp(reg);13 return (control: AbstractControl): { [key: string]: any } | null => {14 control.markAsDirty();15 const NUMBER_REGEXP = new RegExp(match);16 control.markAsTouched();17 if (NUMBER_REGEXP.exec(control.value)) {18 return null;19 }20 return {customPattern: {blank: true}};21 };22 }23 static nameValidator(reg: string = '^[a-zA-Z\u4E00-\u9FFF\u3400-\u4DFF\uF900-\uFAFF ]+$'): ValidatorFn {24 return (control: AbstractControl): { [key: string]: any } | null => {25 control.markAsDirty();26 const NUMBER_REGEXP = new RegExp(reg);27 control.markAsTouched();28 if (NUMBER_REGEXP.exec(control.value)) {29 return null;30 }31 return {customPattern: {name: true}};32 };33 }34 static usernameValidator(): ValidatorFn {35 return (control: AbstractControl): { [key: string]: any } | null => {36 control.markAsDirty();37 const NUMBER_REGEXP = new RegExp(/^([0-9a-zA-Z\u4E00-\u9FFF\u3400-\u4DFF\uF900-\uFAFF_@./#&+-]*)(?:[0-9a-zA-Z\u4E00-\u9FFF\u3400-\u4DFF\uF900-\uFAFF]+)$/);38 control.markAsTouched();39 if (NUMBER_REGEXP.exec(control.value)) {40 return null;41 }42 return {customPattern: {username: true}};43 };44 }45 static numberValidator(reg: string = '^[0-9]*$'): ValidatorFn {46 return (control: AbstractControl): { [key: string]: any } | null => {47 control.markAsDirty();48 const NUMBER_REGEXP = new RegExp(reg);49 control.markAsTouched();50 if (NUMBER_REGEXP.exec(control.value)) {51 return null;52 }53 return {customPattern: {number: true}};54 };55 }56 static emailValidator(reg: string = '^[a-z0-9]+(\.[_a-z0-9]+)*@[a-z0-9-]+(\.[a-z0-9-]+)*(\.[a-z]{2,15})$'): ValidatorFn {57 return (control: AbstractControl): { [key: string]: any } | null => {58 control.markAsDirty();59 const NUMBER_REGEXP = new RegExp(reg);60 control.markAsTouched();61 if (NUMBER_REGEXP.exec(control.value)) {62 return null;63 }64 return {email: true};65 };66 }67 static decimalValidator(allowNegative: boolean = false, allow: number = 4): ValidatorFn {68 const match: RegExp = /^[0-9]+([.,][0-9]{1,4})?$/;69 let NUMBER_REGEXP = new RegExp(match.source.replace('[0-9]', allowNegative ? '-?[0-9]' : '[0-9]'));70 return (control: AbstractControl): { [key: string]: any } | null => {71 control.markAsDirty();72 NUMBER_REGEXP = new RegExp(NUMBER_REGEXP.source.replace('4', allow.toString()));73 control.markAsTouched();74 if (NUMBER_REGEXP.exec(control.value)) {75 return null;76 }77 return {customPattern: {decimal: {upto: allow}}};78 };79 }80 static forbiddenValidator(regExps: Array<RegExp>): ValidatorFn {81 return (control: AbstractControl): { [key: string]: any } | null => {82 const forbidden = [];83 regExps.forEach((regExp: RegExp) => {84 if (regExp.test(control.value)) {85 forbidden.push(regExp.source);86 }87 });88 return forbidden.length > 0 ? {customPattern: {forbidden}} : null;89 };90 }91 static passwordValidator(allow: string = 'all'): ValidatorFn {92 let match: RegExp = /^(?=.*[a-z])(?=.*[A-Z])(?=.*[0-9])(?=.*[!@#\$%\^&\*])/;93 switch (allow) {94 case 'all':95 break;96 case 'alphabet':97 match = /^(?=.*[a-z])/;98 break;99 case 'all-alphabet':100 match = /^(?=.*[a-z])(?=.*[A-Z])/;101 break;102 case 'alphabet-number':103 match = /^(?=.*[a-z])(?=.*[A-Z])(?=.*[0-9])/;104 break;105 case 'alphabet-special-character':106 match = /^(?=.*[a-z])(?=.*[A-Z])(?=.*[!@#\$%\^&\*])/;107 break;108 case 'number-special-character':109 match = /^(?=.*[0-9])(?=.*[!@#\$%\^&\*])/;110 break;111 }112 return (control: AbstractControl): { [key: string]: any } | null => {113 if (control.value.match(match)) {114 return null;115 } else {116 return {customPattern: {password: {type: allow}}};117 }118 };119 }120 static validateAreEqual(controlName: string, matchingControlName: any): (formGroup: FormGroup) => void {121 return (formGroup: FormGroup) => {122 let error = null;123 const control = formGroup.get(controlName);124 const matchingControl = formGroup.get(matchingControlName);125 if (control.value !== matchingControl.value) {126 error = {customPattern: {mustMatch: {[controlName]: true}}};127 } else {128 error = null;129 }130 control.setErrors(error);131 };132 }133 static validateOperation(controlName: string, matchingControlNames: any, operationType: string = 'equal')134 : (formGroup: FormGroup) => void {135 let errorType: string;136 switch (operationType) {137 case 'equal':138 errorType = 'mustNotMatch';139 break;140 case 'less-than':141 errorType = 'mustLessThan';142 break;143 case 'greater-than':144 errorType = 'mustGreaterThan';145 break;146 case 'less-than-equal':147 errorType = 'mustLessThanEqual';148 break;149 case 'greater-than-equal':150 errorType = 'mustGreaterThanEqual';151 break;152 }153 function operation(condition: string, firstValue: any, secondValue: any): boolean {154 let result = true;155 switch (condition) {156 case 'equal':157 result = secondValue === firstValue;158 break;159 case 'less-than':160 result = secondValue < firstValue;161 break;162 case 'greater-than':163 result = secondValue > firstValue;164 break;165 case 'less-than-equal':166 result = secondValue <= firstValue;167 break;168 case 'greater-than-equal':169 result = secondValue >= firstValue;170 break;171 }172 return result;173 }174 return (formGroup: FormGroup) => {175 const control = formGroup.get(controlName);176 let errors;177 errors = control.errors == null ? {customPattern: {[errorType]: []}} : control.errors;178 switch (typeof matchingControlNames) {179 case 'string':180 const matchingControl = formGroup.get(matchingControlNames);181 if (operation(operationType, control.value, matchingControl.value)) {182 if (errors == null) {183 errors = {customPattern: {[errorType]: []}};184 } else {185 if (errors.customPattern) {186 errors.customPattern[errorType] = [];187 } else {188 errors.customPattern = {[errorType]: []};189 }190 }191 errors.customPattern[errorType].push(matchingControlNames);192 control.setErrors(errors[0]);193 } else {194 errors.customPattern[errorType] = [];195 }196 break;197 case 'object':198 matchingControlNames.forEach((matchingControlName: string, index: number) => {199 const matchingControlArray = formGroup.get(matchingControlName);200 if (operation(operationType, control.value, matchingControlArray.value)) {201 if (index === 0) {202 if (errors == null) {203 errors = {customPattern: {[errorType]: []}};204 } else {205 if (errors.customPattern) {206 errors.customPattern[errorType] = [];207 } else {208 errors.customPattern = {[errorType]: []};209 }210 }211 }212 errors.customPattern[errorType] !== undefined213 ? errors.customPattern[errorType].push(matchingControlName)214 : errors.customPattern[errorType] = [matchingControlName];215 }216 });217 break;218 }219 if (errors.customPattern) {220 if (errors.customPattern[errorType]) {221 if (errors.customPattern[errorType].length > 0) {222 control.setErrors(errors);223 }224 } else {225 control.setErrors(control.errors);226 }227 }228 };229 }230 static cardValidator(reg: string = '^[a-zA-Z](?=.{1})+(?=.{9}$)(?:[0-9])+$'): ValidatorFn {231 return (control: AbstractControl): { [key: string]: any } | null => {232 control.markAsDirty();233 const NUMBER_REGEXP = new RegExp(reg);234 control.markAsTouched();235 if (NUMBER_REGEXP.exec(control.value)) {236 return null;237 }238 return {customPattern: {card: true}};239 };240 }241 static ipValidator(reg: string = '^(?=\\d+\\.\\d+\\.\\d+\\.\\d+$)(?:(?:25[0-5]|2[0-4][0-9]|1[0-9]{2}|[1-9][0-9]|[0-9])\\.?){4}$')242 : ValidatorFn {243 return (control: AbstractControl): { [key: string]: any } | null => {244 control.markAsDirty();245 const NUMBER_REGEXP = new RegExp(reg);246 control.markAsTouched();247 if (NUMBER_REGEXP.exec(control.value)) {248 return null;249 }250 return {customPattern: {ip: true}};251 };252 }...
custom-validator.ts
Source:custom-validator.ts
1import { AbstractControl } from '@angular/forms';2export class CustomValidator {3 /**4 * Validates GSTIN5 * @param gstin : Abstract Control6 */7 static GSTINValidators(8 gstin: AbstractControl9 ): { [key: string]: boolean }[] | null {10 if (gstin.pristine) {11 return null;12 }13 var errorDict: { [key: string]: boolean }[] = [];14 const NUMBER_REGEXP = new RegExp('^[0-9]+$');15 const ALPHABET_REGEXP = new RegExp('^[A-Za-z]+$');16 gstin.markAsTouched();17 if (gstin.value.length == 15) {18 const stateCode = gstin.value.slice(0, 2);19 NUMBER_REGEXP.test(stateCode)20 ? null21 : errorDict.push({ invalidStateCode: true });22 const PAN: string = gstin.value.slice(2, 12);23 // First 5 - Alphabets - invalid pan24 const first5 = PAN.slice(0, 5);25 ALPHABET_REGEXP.test(first5)26 ? null27 : errorDict.push({ invalidFirst5PAN: true });28 // Middle 4 - Numeric - invalid pan29 const middle4 = PAN.slice(5, 9);30 NUMBER_REGEXP.test(middle4)31 ? null32 : errorDict.push({ invalidMiddle4PAN: true });33 // Last 1 - Alphabet - invalid pan34 const last1 = PAN.slice(-1);35 ALPHABET_REGEXP.test(last1)36 ? null37 : errorDict.push({ invalidLast1PAN: true });38 const entityNumber = gstin.value.slice(12, 13);39 NUMBER_REGEXP.test(entityNumber)40 ? null41 : errorDict.push({ invalidEntityNumber: true });42 const zAlphabet = gstin.value.slice(13, 14);43 zAlphabet == 'z' || zAlphabet == 'Z'44 ? null45 : errorDict.push({ invalidZAlphabet: true });46 // Checksum GSTIN47 const lastDigit = gstin.value.slice(-1);48 var GSTN_CODEPOINT_CHARS = '0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ';49 var factor = 2;50 var sum = 0;51 var checkCodePoint = 0;52 var mod = GSTN_CODEPOINT_CHARS.length;53 var i;54 for (i = gstin.value.length - 2; i >= 0; i--) {55 var codePoint = -1;56 for (var j = 0; j < GSTN_CODEPOINT_CHARS.length; j++) {57 if (GSTN_CODEPOINT_CHARS[j] === gstin.value[i]) {58 codePoint = j;59 }60 }61 var digit = factor * codePoint;62 factor = factor === 2 ? 1 : 2;63 digit = Math.floor(digit / mod) + (digit % mod);64 sum += digit;65 }66 checkCodePoint = (mod - (sum % mod)) % mod;67 GSTN_CODEPOINT_CHARS[checkCodePoint] == lastDigit68 ? null69 : errorDict.push({ invalidLastDigit: true });70 } else {71 errorDict.push({ invalidGSTIN: true });72 }73 return errorDict.length > 0 ? errorDict : null;74 }75 customGSTValidation(gstin: string): { [key: string]: boolean }[] {76 const NUMBER_REGEXP = new RegExp('^[0-9]+$');77 const ALPHABET_REGEXP = new RegExp('^[A-Za-z]+$');78 // GSTIN Length79 if (gstin.length == 15) {80 // PAN Check81 const GSTINErrorDict: {82 [key: string]: boolean;83 }[] = this.customPANValidation(gstin.slice(2, 12));84 // State Code Check85 const stateCode = gstin.slice(0, 2);86 NUMBER_REGEXP.test(stateCode)87 ? null88 : GSTINErrorDict.push({ invalidStateCode: true });89 // Entity Check90 const entityNumber = gstin.slice(12, 13);91 NUMBER_REGEXP.test(entityNumber)92 ? null93 : GSTINErrorDict.push({ invalidEntityNumber: true });94 // Check Alphabet95 const zAlphabet = gstin.slice(13, 14);96 zAlphabet == 'z' || zAlphabet == 'Z'97 ? null98 : GSTINErrorDict.push({ invalidZAlphabet: true });99 return GSTINErrorDict;100 } else {101 return [{ invalidGSTIN: true }];102 }103 }104 customPANValidation(pan: string): { [key: string]: boolean }[] {105 const NUMBER_REGEXP = new RegExp('^[0-9]+$');106 const ALPHABET_REGEXP = new RegExp('^[A-Za-z]+$');107 if (pan.length == 10) {108 const PANErrorDict: { [key: string]: boolean }[] = [];109 // First 5 - Alphabets - invalid pan110 const first5 = pan.slice(0, 5);111 ALPHABET_REGEXP.test(first5)112 ? null113 : PANErrorDict.push({ invalidFirst5PAN: true });114 // Middle 4 - Numeric - invalid pan115 const middle4 = pan.slice(5, 9);116 NUMBER_REGEXP.test(middle4)117 ? null118 : PANErrorDict.push({ invalidMiddle4PAN: true });119 // Last 1 - Alphabet - invalid pan120 const last1 = pan.slice(-1);121 ALPHABET_REGEXP.test(last1)122 ? null123 : PANErrorDict.push({ invalidLast1PAN: true });124 return PANErrorDict;125 } else {126 return [{ invalidPAN: true }];127 }128 }...
index.js
Source:index.js
1import Decimal from 'decimal.js'2import { calcLMSROutcomeTokenCount } from 'api'3import { LIMIT_MARGIN } from 'utils/constants'4export const NUMBER_REGEXP = /^-?\d+\.?\d*$/5/**6 * Calculates how much outcome tokens you get for value you want to invest7 * @param {Object} market8 * @param {string|number} investment9 * @param {string|number} outcomeIndex10 * @param {Decimal|string|number} limitMargin11 * @returns {Decimal}12 */13const getOutcomeTokenCount = (market, investment, outcomeIndex, limitMargin) => {14 if (!market || !investment || !outcomeIndex || !NUMBER_REGEXP.test(investment) || parseFloat(investment) < 0) {15 return Decimal(0)16 }17 const invest = new Decimal(investment)18 .mul(1e18)19 .div(new Decimal(100).add(limitMargin || LIMIT_MARGIN))20 .mul(100)21 .round()22 const { funding, netOutcomeTokensSold, fee } = market23 let outcomeTokenCount24 try {25 outcomeTokenCount = calcLMSROutcomeTokenCount({26 feeFactor: fee,27 netOutcomeTokensSold,28 funding,29 outcomeTokenIndex: parseInt(outcomeIndex, 10),30 cost: invest.toString(),31 })32 } catch (e) {33 console.error(e)34 return Decimal(0)35 }36 return outcomeTokenCount37}38/**39 * Calculates maximum win amount40 * @param {Decimal|string|number} outcomeTokenCount41 * @param {Decimal|string|number} investment42 * @returns {Decimal}43 */44const getMaximumWin = (outcomeTokenCount, investment) => {45 if (!NUMBER_REGEXP.test(investment) || !parseFloat(investment) > 0 || !outcomeTokenCount) {46 return Decimal(0)47 }48 return Decimal(outcomeTokenCount)49 .sub(Decimal(investment).mul(1e18))50 .div(1e18)51}52/**53 * Calculates maximum return54 * @param {Decimal|string|number} outcomeTokenCount55 * @param {Decimal|string|number} investment56 * @returns {Decimal}57 */58const getPercentageWin = (outcomeTokenCount, investment) => {59 if (!NUMBER_REGEXP.test(investment) || !parseFloat(investment) > 0 || !outcomeTokenCount) {60 return Decimal(0)61 }62 const invest = new Decimal(investment).mul(1e18)63 return Decimal(outcomeTokenCount)64 .div(invest)65 .mul(100)66 .sub(100)67}...
Using AI Code Generation
1import {NUMBER_REGEXP} from 'storybook-root';2import {NUMBER_REGEXP} from 'storybook-root';3import {NUMBER_REGEXP} from 'storybook-root';4import {NUMBER_REGEXP} from 'storybook-root';5import {NUMBER_REGEXP} from 'storybook-root';6import {NUMBER_REGEXP} from 'storybook-root';7import {NUMBER_REGEXP} from 'storybook-root';8import {NUMBER_REGEXP} from 'storybook-root';9import {NUMBER_REGEXP} from 'storybook-root';10import {NUMBER_REGEXP} from 'storybook-root';11import {NUMBER_REGEXP} from 'storybook-root';12import {NUMBER_REGEXP} from 'storybook-root';13import {NUMBER_REGEXP} from 'storybook-root';14import {NUMBER_REGEXP} from 'storybook-root';15import {NUMBER_REGEXP} from 'storybook-root';16import {NUMBER_REGEXP} from 'storybook-root';17import {NUMBER_REGEXP} from 'storybook-root';18import {NUMBER_REGEXP} from 'storybook-root';19import {NUMBER_REGEXP} from 'storybook-root';
Using AI Code Generation
1import { NUMBER_REGEXP } from 'storybook-root';2export const test = () => {3 console.log(NUMBER_REGEXP);4};5test();6{7 "devDependencies": {8 }9}10export const NUMBER_REGEXP = /^-?(\d+|\d{1,3}(,\d{3})*)(\.\d+)?%?$/;11{12}
Using AI Code Generation
1import { NUMBER_REGEXP } from 'storybook-root';2console.log(NUMBER_REGEXP.test('123'));3const path = require('path');4module.exports = (baseConfig, env, config) => {5 config.resolve.alias['storybook-root'] = path.resolve(__dirname, '../');6 return config;7};
Using AI Code Generation
1import { NUMBER_REGEXP } from 'storybook-root'2import { NUMBER_REGEXP } from 'storybook-root/src/utils/constant'3import { NUMBER_REGEXP } from 'storybook-root/utils/constant'4import { NUMBER_REGEXP } from 'storybook-root/src/utils/constant'5import { NUMBER_REGEXP } from 'storybook-root/utils/constant'6import { NUMBER_REGEXP } from 'storybook-root/src/utils/constant'7import { NUMBER_REGEXP } from 'storybook-root/utils/constant'8import { NUMBER_REGEXP } from 'storybook-root/src/utils/constant'9import { NUMBER_REGEXP } from 'storybook-root/utils/constant'10import { NUMBER_REGEXP } from 'storybook-root/src/utils/constant'11import { NUMBER_REGEXP } from 'storybook-root/utils/constant'12import { NUMBER_REGEXP } from 'storybook-root/src/utils/constant'13import { NUMBER_REGEXP } from 'storybook-root/utils/constant'14import { NUMBER_REGEXP } from 'storybook-root/src/utils/constant'15import { NUMBER_REGEXP } from 'storybook-root/utils/constant'16import { NUMBER_REGEXP }
Using AI Code Generation
1import { NUMBER_REGEXP } from 'storybook-root';2const number = 123;3const isNumber = NUMBER_REGEXP.test(number);4console.log(isNumber);5import { NUMBER_REGEXP } from 'storybook-root/src/other';6const number = 123;7const isNumber = NUMBER_REGEXP.test(number);8console.log(isNumber);9We also learned about the different ways of importing files from the root directory and from the other directories. We also learned about the different ways of importing files from the
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!!