Best JavaScript code snippet using storybook-root
str_slug.js
Source:str_slug.js
1/** translation of Taylor Otwell's str_slug helper from Laravel **/2/** https://gist.github.com/Lucassifoni/0e94e03a63b53518ddf466f22aa1cf19 **/3/**4 * Trims a string for a given start/end character5 * @param {string} string6 * @param {string} pattern7 */8const trimChar = function trimChar(string, pattern) {9 if (typeof pattern.toString === 'undefined') {10 throw new Error('pattern must be a string or castable to string');11 }12 if (typeof string.toString === 'undefined') {13 throw new Error('string must be a string or castable to string');14 }15 let str = string.toString();16 let needle = pattern.toString();17 while (str.charAt(0) === needle) {18 str = str.substring(1);19 }20 while (str.charAt(str.length - 1) === needle) {21 str = str.substring(0, str.length - 1);22 }23 return str;24};25/**26 * Replaces all occurences of all given patterns with a single replacement27 * @param {string} string28 * @param {array} patternArray29 * @param {string} replacement30 * @returns {string} str31 */32const arrayReplaceSingle = function arrayReplaceSingle(string, patternArray, replacement) {33 if (typeof string.toString === 'undefined') {34 throw new Error('string parameter must be a string or cast-able to string.');35 }36 const l = patternArray.length;37 if (l === 0) {38 throw new Error('Patterns array must have members.');39 }40 let str = string.toString();41 for (let i = 0; i < l; i += 1) {42 if (typeof patternArray[i].toString === 'undefined') {43 throw new Error('Every pattern array member must be a string or cast-able to string');44 }45 str = str.replace(new RegExp(patternArray[i], 'g'), replacement);46 }47 return str;48};49/**50 * CharMap allowing language-aware replacements51 * @type {object}52 */53const charMap = {54 '0': ['°', 'â', 'Û°', 'ï¼'],55 '1': ['¹', 'â', 'Û±', 'ï¼'],56 '2': ['²', 'â', 'Û²', 'ï¼'],57 '3': ['³', 'â', 'Û³', 'ï¼'],58 '4': ['â´', 'â', 'Û´', 'Ù¤', 'ï¼'],59 '5': ['âµ', 'â
', 'Ûµ', 'Ù¥', 'ï¼'],60 '6': ['â¶', 'â', 'Û¶', 'Ù¦', 'ï¼'],61 '7': ['â·', 'â', 'Û·', 'ï¼'],62 '8': ['â¸', 'â', 'Û¸', 'ï¼'],63 '9': ['â¹', 'â', 'Û¹', 'ï¼'],64 'a': ['à ', 'á', 'ả', 'ã', 'ạ', 'Ä', 'ắ', 'ằ', 'ẳ', 'ẵ', 'ặ', 'â', 'ấ', 'ầ', 'ẩ', 'ẫ', 'áº', 'Ä', 'Ä
', 'Ã¥', 'α', 'ά', 'á¼', 'á¼', 'á¼', 'á¼', 'á¼', 'á¼
', 'á¼', 'á¼', 'á¾', 'á¾', 'á¾', 'á¾', 'á¾', 'á¾
', 'á¾', 'á¾', 'á½°', 'ά', 'á¾°', 'á¾±', 'á¾²', 'á¾³', 'á¾´', 'ᾶ', 'á¾·', 'а', 'Ø£', 'á¡', 'á¬', 'á«', 'Ç»', 'Ç', 'ª', 'á', 'à¤
', 'ا', 'ï½', 'ä'],65 'b': ['б', 'β', 'ب', 'á', 'á', 'ï½'],66 'c': ['ç', 'Ä', 'Ä', 'Ä', 'Ä', 'ï½'],67 'd': ['Ä', 'ð', 'Ä', 'Æ', 'È¡', 'É', 'É', 'áµ', 'á¶', 'á¶', 'д', 'δ', 'د', 'ض', 'á', 'á', 'á', 'ï½'],68 'e': ['é', 'è', 'ẻ', 'ẽ', 'ẹ', 'ê', 'ế', 'á»', 'á»', 'á»
', 'á»', 'ë', 'Ä', 'Ä', 'Ä', 'Ä', 'Ä', 'ε', 'Î', 'á¼', 'á¼', 'á¼', 'á¼', 'á¼', 'á¼', 'á½²', 'Î', 'е', 'Ñ', 'Ñ', 'Ñ', 'É', 'á§', 'á±', 'á²', 'á', 'à¤', 'Ø¥', 'ئ', 'ï½
'],69 'f': ['Ñ', 'Ï', 'Ù', 'Æ', 'á¤', 'ï½'],70 'g': ['Ä', 'Ä', 'Ä¡', 'Ä£', 'г', 'Ò', 'γ', 'á', 'á', 'Ú¯', 'ï½'],71 'h': ['Ä¥', 'ħ', 'η', 'ή', 'Ø', 'Ù', 'á', 'á¾', 'á°', 'ï½'],72 'i': ['Ã', 'ì', 'á»', 'Ä©', 'á»', 'î', 'ï', 'Ä«', 'Ä', 'į', 'ı', 'ι', 'ί', 'Ï', 'Î', 'á¼°', 'á¼±', 'á¼²', 'á¼³', 'á¼´', 'á¼µ', 'ἶ', 'á¼·', 'ὶ', 'ί', 'á¿', 'á¿', 'á¿', 'Î', 'á¿', 'á¿', 'Ñ', 'Ñ', 'и', 'á£', 'á', 'á®', 'ááº', 'Ç', 'á', 'à¤', 'Û', 'ï½'],73 'j': ['ĵ', 'Ñ', 'Ð', 'á¯', 'ج', 'ï½'],74 'k': ['Ä·', 'ĸ', 'к', 'κ', 'Ķ', 'Ù', 'Ù', 'á', 'á', 'á¥', 'Ú©', 'ï½'],75 'l': ['Å', 'ľ', 'ĺ', 'ļ', 'Å', 'л', 'λ', 'Ù', 'á', 'á', 'ï½'],76 'm': ['м', 'μ', 'Ù
', 'á', 'á', 'ï½'],77 'n': ['ñ', 'Å', 'Å', 'Å', 'Å', 'Å', 'ν', 'н', 'Ù', 'á', 'á', 'ï½'],78 'o': ['ó', 'ò', 'á»', 'õ', 'á»', 'ô', 'á»', 'á»', 'á»', 'á»', 'á»', 'Æ¡', 'á»', 'á»', 'á»', 'ỡ', 'ợ', 'ø', 'Å', 'Å', 'Å', 'ο', 'á½', 'á½', 'á½', 'á½', 'á½', 'á½
', 'ὸ', 'Ï', 'о', 'Ù', 'θ', 'áá¯', 'Ç', 'Ç¿', 'º', 'á', 'à¤', 'ï½', 'ö'],79 'p': ['п', 'Ï', 'á', 'á', 'Ù¾', 'ï½'],80 'q': ['á§', 'ï½'],81 'r': ['Å', 'Å', 'Å', 'Ñ', 'Ï', 'ر', 'á ', 'ï½'],82 's': ['Å', 'Å¡', 'Å', 'Ñ', 'Ï', 'È', 'Ï', 'س', 'ص', 'á
', 'Å¿', 'á¡', 'ï½'],83 't': ['Å¥', 'Å£', 'Ñ', 'Ï', 'È', 'ت', 'Ø·', 'á', 'á', 'ŧ', 'á', 'á¢', 'ï½'],84 'u': ['ú', 'ù', 'ủ', 'Å©', 'ụ', 'Æ°', 'ứ', 'ừ', 'á»', 'ữ', 'á»±', 'û', 'Å«', 'ů', 'ű', 'Å', 'ų', 'µ', 'Ñ', 'á', 'á¯', 'á°', 'Ç', 'Ç', 'Ç', 'Ç', 'Ç', 'á£', 'à¤', 'ï½', 'Ñ', 'ü'],85 'v': ['в', 'á', 'Ï', 'ï½'],86 'w': ['ŵ', 'Ï', 'Ï', 'á', 'á½', 'ï½'],87 'x': ['Ï', 'ξ', 'ï½'],88 'y': ['ý', 'ỳ', 'á»·', 'ỹ', 'ỵ', 'ÿ', 'Å·', 'й', 'Ñ', 'Ï
', 'Ï', 'Ï', 'ΰ', 'Ù', 'á', 'ï½'],89 'z': ['ź', 'ž', 'ż', 'з', 'ζ', 'ز', 'á', 'á', 'ï½'],90 'aa': ['ع', 'à¤', 'Ø¢'],91 'ae': ['æ', 'ǽ'],92 'ai': ['à¤'],93 'ch': ['Ñ', 'á©', 'á', 'Ú'],94 'dj': ['Ñ', 'Ä'],95 'dz': ['Ñ', 'á«'],96 'ei': ['à¤'],97 'gh': ['غ', 'á¦'],98 'ii': ['à¤'],99 'ij': ['ij'],100 'kh': ['Ñ
', 'Ø®', 'á®'],101 'lj': ['Ñ'],102 'nj': ['Ñ'],103 'oe': ['ö', 'Å', 'ؤ'],104 'oi': ['à¤'],105 'oii': ['à¤'],106 'ps': ['Ï'],107 'sh': ['Ñ', 'á¨', 'Ø´'],108 'shch': ['Ñ'],109 'ss': ['Ã'],110 'sx': ['Å'],111 'th': ['þ', 'Ï', 'Ø«', 'Ø°', 'ظ'],112 'ts': ['Ñ', 'áª', 'á¬'],113 'ue': ['ü'],114 'uu': ['à¤'],115 'ya': ['Ñ'],116 'yu': ['Ñ'],117 'zh': ['ж', 'á', 'Ú'],118 '(c)': ['©'],119 'A': ['Ã', 'Ã', 'Ả', 'Ã', 'Ạ', 'Ä', 'Ắ', 'Ằ', 'Ẳ', 'Ẵ', 'Ặ', 'Ã', 'Ấ', 'Ầ', 'Ẩ', 'Ẫ', 'Ậ', 'Ã
', 'Ä', 'Ä', 'Î', 'Î', 'á¼', 'á¼', 'á¼', 'á¼', 'á¼', 'á¼', 'á¼', 'á¼', 'á¾', 'á¾', 'á¾', 'á¾', 'á¾', 'á¾', 'á¾', 'á¾', 'Ᾰ', 'á¾¹', 'Ὰ', 'Î', 'á¾¼', 'Ð', 'Ǻ', 'Ç', 'A', 'Ã'],120 'B': ['Ð', 'Î', 'ब', 'ï¼¢'],121 'C': ['Ã', 'Ä', 'Ä', 'Ä', 'Ä', 'ï¼£'],122 'D': ['Ä', 'Ã', 'Ä', 'Æ', 'Æ', 'Æ', 'á´
', 'á´', 'Ð', 'Î', 'D'],123 'E': ['Ã', 'Ã', 'Ẻ', 'Ẽ', 'Ẹ', 'Ã', 'Ế', 'á»', 'á»', 'á»', 'á»', 'Ã', 'Ä', 'Ä', 'Ä', 'Ä', 'Ä', 'Î', 'Î', 'á¼', 'á¼', 'á¼', 'á¼', 'á¼', 'á¼', 'Î', 'á¿', 'Ð', 'Ð', 'Ð', 'Ð', 'Æ', 'ï¼¥'],124 'F': ['Ф', 'Φ', 'F'],125 'G': ['Ä', 'Ä ', 'Ä¢', 'Ð', 'Ò', 'Î', 'G'],126 'H': ['Î', 'Î', 'Ħ', 'H'],127 'I': ['Ã', 'Ã', 'á»', 'Ĩ', 'á»', 'Ã', 'Ã', 'Ī', 'Ĭ', 'Ä®', 'Ä°', 'Î', 'Î', 'Ϊ', 'Ἰ', 'á¼¹', 'á¼»', 'á¼¼', 'á¼½', 'á¼¾', 'Ἷ', 'á¿', 'á¿', 'á¿', 'Î', 'Ð', 'Ð', 'Ð', 'Ç', 'Ï', 'I'],128 'J': ['J'],129 'K': ['Ð', 'Î', 'K'],130 'L': ['Ĺ', 'Å', 'Ð', 'Î', 'Ä»', 'Ľ', 'Ä¿', 'ल', 'L'],131 'M': ['Ð', 'Î', 'ï¼'],132 'N': ['Å', 'Ã', 'Å', 'Å
', 'Å', 'Ð', 'Î', 'ï¼®'],133 'O': ['Ã', 'Ã', 'á»', 'Ã', 'á»', 'Ã', 'á»', 'á»', 'á»', 'á»', 'á»', 'Æ ', 'á»', 'á»', 'á»', 'á» ', 'Ợ', 'Ã', 'Å', 'Å', 'Å', 'Î', 'Î', 'á½', 'á½', 'á½', 'á½', 'á½', 'á½', 'Ὸ', 'Î', 'Ð', 'Î', 'Ó¨', 'Ç', 'Ǿ', 'O', 'Ã'],134 'P': ['Ð', 'Î ', 'ï¼°'],135 'Q': ['ï¼±'],136 'R': ['Å', 'Å', 'Ð ', 'Ρ', 'Å', 'ï¼²'],137 'S': ['Å', 'Å', 'È', 'Å ', 'Å', 'С', 'Σ', 'ï¼³'],138 'T': ['Ť', 'Å¢', 'Ŧ', 'È', 'Т', 'Τ', 'ï¼´'],139 'U': ['Ã', 'Ã', 'Ủ', 'Ũ', 'Ụ', 'Ư', 'Ứ', 'Ừ', 'Ử', 'á»®', 'á»°', 'Ã', 'Ū', 'Å®', 'Å°', 'Ŭ', 'Ų', 'У', 'Ç', 'Ç', 'Ç', 'Ç', 'Ç', 'ï¼µ', 'Ð', 'Ã'],140 'V': ['Ð', 'V'],141 'W': ['Ω', 'Î', 'Å´', 'ï¼·'],142 'X': ['Χ', 'Î', 'X'],143 'Y': ['Ã', 'Ỳ', 'Ỷ', 'Ỹ', 'á»´', 'Ÿ', 'Ῠ', 'á¿©', 'Ὺ', 'Î', 'Ы', 'Ð', 'Î¥', 'Ϋ', 'Ŷ', 'ï¼¹'],144 'Z': ['Ź', 'Ž', 'Å»', 'Ð', 'Î', 'Z'],145 'AE': ['Ã', 'Ǽ'],146 'Ch': ['Ч'],147 'Dj': ['Ð'],148 'Dz': ['Ð'],149 'Gx': ['Ä'],150 'Hx': ['Ĥ'],151 'Ij': ['IJ'],152 'Jx': ['Ä´'],153 'Kh': ['Ð¥'],154 'Lj': ['Ð'],155 'Nj': ['Ð'],156 'Oe': ['Å'],157 'Ps': ['Ψ'],158 'Sh': ['Ш'],159 'Shch': ['Щ'],160 'Ss': ['áº'],161 'Th': ['Ã'],162 'Ts': ['Ц'],163 'Ya': ['Я'],164 'Yu': ['Ю'],165 'Zh': ['Ð'],166 ' ': ["\xC2\xA0", "\xE2\x80\x80", "\xE2\x80\x81", "\xE2\x80\x82", "\xE2\x80\x83", "\xE2\x80\x84", "\xE2\x80\x85", "\xE2\x80\x86", "\xE2\x80\x87", "\xE2\x80\x88", "\xE2\x80\x89", "\xE2\x80\x8A", "\xE2\x80\xAF", "\xE2\x81\x9F", "\xE3\x80\x80", "\xEF\xBE\xA0"],167};168/**169 * Bulgar & German specific charmaps170 * @type {{bg: *[], de: *[]}}171 */172const languageSpecific = {173 'bg': [174 ['Ñ
', 'Ð¥', 'Ñ', 'Щ', 'Ñ', 'Ъ', 'Ñ', 'Ь'],175 ['h', 'H', 'sht', 'SHT', 'a', 'Ð', 'y', 'Y'],176 ],177 'de': [178 ['ä', 'ö', 'ü', 'Ã', 'Ã', 'Ã'],179 ['ae', 'oe', 'ue', 'AE', 'OE', 'UE'],180 ],181};182/**183 * Returns a language-specific charmap for German and Bulgare184 * @param {string} language185 * @returns {Array | null}186 */187const languageSpecificCharsArray = function languageSpecificCharsArray(language) {188 if (typeof language.toString === 'undefined') {189 throw new Error('language parameter must be a string or castable to string');190 }191 const l = languageSpecific[language.toString()];192 if (typeof l !== 'undefined') {193 return l;194 }195 return null;196};197/**198 * Ascii-fies a given string with a language-specific phonetic replacement199 * @param {string} string200 * @param {string} language201 * @returns {string | * | void}202 */203const ascii = function ascii(string, language = 'en') {204 if (typeof language.toString === 'undefined') {205 throw new Error('language parameter must be a string or castable to string');206 }207 const languageSpecific = languageSpecificCharsArray(language.toString());208 let str = string;209 if (languageSpecific !== null) {210 str = string.replace(languageSpecific[0], languageSpecific[1]);211 }212 const charKeys = Object.keys(charMap);213 charKeys.forEach((key) => {214 str = arrayReplaceSingle(str, charMap[key], key);215 });216 return str.replace(/[^\x20-\x7E]/g, '');217};218/**219 * Slugifies a string according to the str_slug laravel helper220 * @param {string} string221 * @param {string} separator222 * @param {string} language223 * @returns {string | *}224 */225const str_slug = function str_slug(string, separator = '-', language = 'en') {226 let str = ascii(string);227 if (typeof str.toString === 'undefined') {228 throw new Error('An error has occured while converting the input string to ascii.');229 }230 str = str.toString();231 const flip = separator === '-' ? '_' : '-';232 str = str.replace(new RegExp(flip, 'g'), separator);233 str = str.replace(new RegExp('@', 'g'), `${separator}at${separator}`);234 str = str.replace('/![^' + separator + '\w\d\s/g', '');235 str = str.toLowerCase();236 str = str.replace(/[\s]/g, separator);237 // str = trimChar(str, '-');238 return str;239};...
abstract-resource-preference-provider.ts
Source:abstract-resource-preference-provider.ts
1import * as jsoncparser from 'jsonc-parser';2import { Injectable, Autowired } from '@opensumi/di';3import {4 JSONUtils,5 URI,6 Disposable,7 isUndefined,8 PreferenceProviderDataChanges,9 ILogger,10 IResolvedPreferences,11 Throttler,12 FileChange,13 Schemes,14} from '@opensumi/ide-core-browser';15import {16 PreferenceProvider,17 PreferenceSchemaProvider,18 PreferenceScope,19 PreferenceProviderDataChange,20 PreferenceConfigurations,21} from '@opensumi/ide-core-browser';22import { IFileServiceClient } from '@opensumi/ide-file-service';23import { IPreferenceTask } from '../common';24// vscode 对è¯è¨çsettingæ¯æ ¹æ®è¿ç§æ ¼å¼æ¥ç25// "[json]": { "editor.formatter": "xxxx" }26// 对å
¶è¿è¡å
¼å®¹27const OVERRIDE_PROPERTY = '\\[(.*)\\]$';28export const OVERRIDE_PROPERTY_PATTERN = new RegExp(OVERRIDE_PROPERTY);29@Injectable()30export abstract class AbstractResourcePreferenceProvider extends PreferenceProvider {31 protected preferences: IResolvedPreferences = {32 default: {},33 languageSpecific: {},34 };35 @Autowired(PreferenceSchemaProvider)36 protected readonly schemaProvider: PreferenceSchemaProvider;37 @Autowired(PreferenceConfigurations)38 protected readonly configurations: PreferenceConfigurations;39 @Autowired(IFileServiceClient)40 protected readonly fileSystem: IFileServiceClient;41 @Autowired(ILogger)42 private logger: ILogger;43 private preferenceThrottler: Throttler = new Throttler();44 private preferenceTasks: IPreferenceTask[] = [];45 constructor() {46 super();47 this.listen();48 }49 protected listen() {50 if (this.fileSystem.handlesScheme(Schemes.file) && this.fileSystem.handlesScheme(Schemes.userStorage)) {51 this.init();52 } else {53 const disposable = this.fileSystem.onFileProviderChanged((scheme: string[]) => {54 if (this.fileSystem.handlesScheme(Schemes.file) && this.fileSystem.handlesScheme(Schemes.userStorage)) {55 this.init();56 disposable.dispose();57 }58 });59 }60 }61 protected async init(): Promise<void> {62 // å°è¯è¯»åpreferencesåå§å
容63 this.readPreferences()64 .then(() => this._ready.resolve())65 .catch(() => this._ready.resolve());66 const uri = this.getUri();67 const watcher = await this.fileSystem.watchFileChanges(uri);68 // é
ç½®æ件æ¹åæ¶ï¼éæ°è¯»åé
ç½®69 this.toDispose.push(watcher);70 watcher.onFilesChanged((e: FileChange[]) => {71 const effected = e.find((file) => file.uri === uri.toString());72 if (effected) {73 return this.readPreferences();74 }75 });76 this.toDispose.push(Disposable.create(() => this.reset()));77 }78 protected abstract getUri(): URI;79 protected abstract getScope(): PreferenceScope;80 getConfigUri(): URI;81 getConfigUri(resourceUri: string | undefined): URI | undefined;82 getConfigUri(resourceUri?: string): URI | undefined {83 if (!resourceUri) {84 return this.getUri();85 }86 // è·åconfigUriä¸éè¦çå¾
é
置读åå®åºè¯¥å°±å¯ä»¥è¯»å87 return this.contains(resourceUri) ? this.getUri() : undefined;88 }89 contains(resourceUri: string | undefined): boolean {90 if (!resourceUri) {91 return true;92 }93 const domain = this.getDomain();94 if (!domain) {95 return true;96 }97 const resourcePath = new URI(resourceUri).path;98 return domain.some((uri) => new URI(uri).path.relativity(resourcePath) >= 0);99 }100 getPreferences(resourceUri?: string, language?: string) {101 return this.loaded && this.contains(resourceUri) ? this.getOnePreference(language) : undefined;102 }103 getLanguagePreferences(resourceUri?: string) {104 return this.loaded && this.contains(resourceUri) ? this.preferences.languageSpecific : undefined;105 }106 getOnePreference(language?: string): { [key: string]: any } {107 if (language) {108 return this.preferences.languageSpecific[language] || {};109 } else {110 return this.preferences.default;111 }112 }113 async resolvePreferenceTasks(tasks: IPreferenceTask[]) {114 const uri = this.getUri();115 // 读åé
ç½®æ¶åæ¶æ´æ°ä¸ä¸èµæºä¿¡æ¯ï¼é²æ¢åå
¥æ¶å¯¹å¼æ¥åå
¥æ
åµçé误å¤æ116 this.resource = this.fileSystem.getFileStat(uri.toString());117 let resource = await this.resource;118 let content = ((await this.readContents()) || '').trim();119 // å°å¤æ¬¡é
置修æ¹å并为ä¸æ¬¡æ件å
容åæ´120 for (const task of tasks) {121 const { path, value } = task;122 if ((!content || path.length === 0) && isUndefined(value)) {123 continue;124 }125 const formattingOptions = { tabSize: 2, insertSpaces: true, eol: '' };126 const edits = jsoncparser.modify(content, path, value, { formattingOptions });127 content = jsoncparser.applyEdits(content, edits);128 }129 try {130 if (!resource) {131 // å½èµæºä¸åå¨åéè¦åå
¥æ°æ®æ¶ï¼å建对åºæ件132 resource = await this.fileSystem.createFile(uri.toString());133 }134 await this.fileSystem.setContent(resource, content);135 await this.readPreferences(content);136 return true;137 } catch (e) {138 this.logger.error(`${e.toString()}`);139 return false;140 }141 }142 /**143 * é
ç½®åæ´éåå¤çå½æ°144 */145 doSetPreferenceTask() {146 const tasks = this.preferenceTasks.slice(0);147 this.preferenceTasks = [];148 return this.resolvePreferenceTasks(tasks);149 }150 async doSetPreference(key: string, value: any, resourceUri?: string, language?: string): Promise<boolean> {151 if (!this.contains(resourceUri)) {152 return false;153 }154 const path = this.getPath(key, language);155 if (!path) {156 return false;157 }158 // è¿éå°æ¯æ¬¡é
ç½®åæ´çåæ°æé 为ä¸ä¸ª IPreferenceTask159 this.preferenceTasks.push({ path, key, value });160 return await this.preferenceThrottler.queue<boolean>(this.doSetPreferenceTask.bind(this));161 }162 protected getPath(preferenceName: string, language?: string): string[] | undefined {163 if (language) {164 return [`[${language}]`, preferenceName];165 }166 return [preferenceName];167 }168 protected loaded = false;169 protected async readPreferences(content?: string): Promise<void> {170 const newContent = content || (await this.readContents());171 this.loaded = !isUndefined(newContent);172 const newPrefs = newContent ? this.getParsedContent(newContent) : { default: {}, languageSpecific: {} };173 this.handlePreferenceChanges(newPrefs);174 }175 protected async readContents(): Promise<string | undefined> {176 try {177 const uri = this.getUri();178 const { content } = await this.fileSystem.readFile(uri.toString());179 return content.toString();180 } catch (e) {181 return undefined;182 }183 }184 protected getParsedContent(content: string): IResolvedPreferences {185 const jsonData = this.parse(content);186 const preferences: IResolvedPreferences = {187 default: {},188 languageSpecific: {},189 };190 if (typeof jsonData !== 'object') {191 return preferences;192 }193 for (const preferenceName of Object.keys(jsonData)) {194 const preferenceValue = jsonData[preferenceName];195 // è¿éç±äºæ件çschema注åè¾æï¼å¨ç¬¬ä¸æ¬¡è·åé
ç½®æ¶ä¼æ ¡éªä¸éè¿å¯¼è´åä¸å°å¼ï¼è¯»åææ¶å»ææ ¡éªé»è¾196 if (OVERRIDE_PROPERTY_PATTERN.test(preferenceName)) {197 const language = preferenceName.match(OVERRIDE_PROPERTY_PATTERN)![1];198 preferences.languageSpecific[language] = preferences.languageSpecific[language] || {};199 // eslint-disable-next-line guard-for-in200 for (const overriddenPreferenceName in preferenceValue) {201 const overriddenValue = preferenceValue[overriddenPreferenceName];202 preferences.languageSpecific[language][`${overriddenPreferenceName}`] = overriddenValue;203 }204 } else {205 preferences.default[preferenceName] = preferenceValue;206 }207 }208 return preferences;209 }210 protected validate(preferenceName: string, preferenceValue: any): boolean {211 if (this.configurations.getPath(this.getUri()) !== this.configurations.getPaths()[0]) {212 return true;213 }214 return isUndefined(preferenceValue) || this.schemaProvider.validate(preferenceName, preferenceValue).valid;215 }216 protected parse(content: string): any {217 content = content.trim();218 if (!content) {219 return undefined;220 }221 const strippedContent = jsoncparser.stripComments(content);222 return jsoncparser.parse(strippedContent);223 }224 protected handlePreferenceChanges(newPrefs: IResolvedPreferences): void {225 const oldPrefs = Object.assign({}, this.preferences);226 this.preferences = newPrefs;227 const changes: PreferenceProviderDataChanges = this.collectChanges(this.preferences, oldPrefs);228 if (Object.keys(changes.default).length > 0 || Object.keys(changes.languageSpecific).length > 0) {229 this.emitPreferencesChangedEvent(changes);230 }231 }232 protected reset(): void {233 const preferences = this.preferences;234 this.preferences = { default: {}, languageSpecific: {} };235 const changes: PreferenceProviderDataChanges = this.collectChanges(this.preferences, preferences);236 if (Object.keys(changes.default).length > 0 || Object.keys(changes.languageSpecific).length > 0) {237 this.emitPreferencesChangedEvent(changes);238 }239 }240 private collectChanges(newPref: IResolvedPreferences, oldPref: IResolvedPreferences): PreferenceProviderDataChanges {241 const changes: PreferenceProviderDataChanges = {242 default: this.collectOneChanges(newPref.default, oldPref.default),243 languageSpecific: {},244 };245 const languages = new Set<string>([246 ...Object.keys(newPref.languageSpecific),247 ...Object.keys(oldPref.languageSpecific),248 ]);249 for (const language of languages) {250 const languageChange = this.collectOneChanges(251 newPref.languageSpecific[language],252 oldPref.languageSpecific[language],253 );254 if (Object.keys(languageChange).length > 0) {255 changes.languageSpecific[language] = languageChange;256 }257 }258 return changes;259 }260 private collectOneChanges(261 newPref: { [name: string]: any },262 oldPref: { [name: string]: any },263 ): { [preferenceName: string]: PreferenceProviderDataChange } {264 const keys = new Set([...Object.keys(oldPref || {}), ...Object.keys(newPref || {})]);265 const changes: { [preferenceName: string]: PreferenceProviderDataChange } = {};266 const uri = this.getUri();267 for (const prefName of keys) {268 const oldValue = oldPref[prefName];269 const newValue = newPref[prefName];270 const schemaProperties = this.schemaProvider.getCombinedSchema().properties[prefName];271 if (schemaProperties) {272 const scope = schemaProperties.scope;273 // do not emit the change event if the change is made out of the defined preference scope274 if (!this.schemaProvider.isValidInScope(prefName, this.getScope())) {275 this.logger.warn(276 `Preference ${prefName} in ${uri} can only be defined in scopes: ${PreferenceScope.getScopeNames(277 scope,278 ).join(', ')}.`,279 );280 continue;281 }282 }283 if (284 (isUndefined(newValue) && oldValue !== newValue) ||285 (isUndefined(oldValue) && newValue !== oldValue) || // JSONUtils.deepEqual() does not support handling `undefined`286 !JSONUtils.deepEqual(oldValue, newValue)287 ) {288 changes[prefName] = {289 preferenceName: prefName,290 newValue,291 oldValue,292 scope: this.getScope(),293 domain: this.getDomain(),294 };295 }296 }297 return changes;298 }...
upperCase.ts
Source:upperCase.ts
1import { LanguageSpecific } from "./types.ts";2const LANGUAGES: LanguageSpecific = {3 tr: {4 regexp: /[\u0069]/g,5 map: {6 i: "\u0130",7 },8 },9 az: {10 regexp: /[\u0069]/g,11 map: {12 i: "\u0130",13 },14 },15 lt: {16 regexp: /[\u0069\u006A\u012F]\u0307|\u0069\u0307[\u0300\u0301\u0303]/g,17 map: {18 iÌ: "\u0049",19 jÌ: "\u004A",20 įÌ: "\u012E",21 iÌÌ: "\u00CC",22 iÌÌ: "\u00CD",23 iÌÌ: "\u0128",24 },25 },26};27export default function (str: string, locale?: string): string {28 str = str == null ? "" : String(str);29 if (!locale) {30 return str.toUpperCase();31 }32 const lang = LANGUAGES[locale];33 if (lang) {34 str = str.replace(lang.regexp, function (m) {35 return lang.map[m];36 });37 }38 return str.toUpperCase();...
Using AI Code Generation
1import { languageSpecific } from 'storybook-root';2import { storiesOf } from '@storybook/react';3import { withKnobs, text } from '@storybook/addon-knobs';4const stories = storiesOf('Test', module);5stories.addDecorator(withKnobs);6stories.add('with text', () => {7 const label = languageSpecific('test.label', 'label');8 const value = text(label, 'Hello Storybook');9 return <div>{value}</div>;10});11import { addLocaleData } from 'react-intl';12import en from 'react-intl/locale-data/en';13import de from 'react-intl/locale-data/de';14addLocaleData([...en, ...de]);15const enMessages = require('./locales/en.json');16const deMessages = require('./locales/de.json');17const messages = {18};19export const languageSpecific = (key, defaultMessage) => {20 return messages[language][key] || defaultMessage;21};
Using AI Code Generation
1import { languageSpecific } from 'storybook-root';2import { storiesOf } from '@storybook/react';3const stories = storiesOf('Test', module);4stories.add('test', () => {5 return languageSpecific(6 {7 },8 );9});10import { languageSpecific } from 'storybook-root';11import { addDecorator } from '@storybook/react';12addDecorator(languageSpecific);13import { addParameters } from '@storybook/react';14addParameters({15 languages: {16 },17});18import { addons } from '@storybook/addons';19addons.setConfig({20});21import 'storybook-root/register';22module.exports = {23};24const path = require('path');25module.exports = ({ config }) => {26 config.resolve.alias['storybook-root'] = path.resolve(__dirname, '../');27 return config;28};29module.exports = {30 typescript: {31 },32};33module.exports = {34 typescript: {35 },36};37module.exports = {38 typescript: {39 },40};
Using AI Code Generation
1var storybookRoot = require("storybook-root");2storybookRoot.languageSpecific("en", "es", "zh");3var storybookRoot = require("storybook-root");4storybookRoot.languageSpecific("en", "es", "zh");5var storybookRoot = require("storybook-root");6storybookRoot.languageSpecific("en", "es", "zh");7{ en: 'en', es: 'es', zh: 'zh' }8{ en: 'en', es: 'es', zh: 'zh' }9{ en: 'en', es: 'es', zh: 'zh' }10{ en: 'en', es: 'es', zh: 'zh' }11{ en: 'en', es: 'es', zh: 'zh' }12{ en: 'en', es: 'es', zh: 'zh' }13{ en: 'en', es: 'es', zh: 'zh' }14{ en: 'en', es: 'es', zh: 'zh' }15{ en: 'en', es: 'es', zh: 'zh' }
Using AI Code Generation
1import { languageSpecific } from 'storybook-root-decorator';2import { languageSpecific } from 'storybook-root-decorator';3import { languageSpecific } from 'storybook-root-decorator';4import { languageSpecific } from 'storybook-root-decorator';5import { languageSpecific } from 'storybook-root-decorator';6import { languageSpecific } from 'storybook-root-decorator';7import { languageSpecific } from 'storybook-root-decorator';
Using AI Code Generation
1import { languageSpecific } from 'storybook-root'2languageSpecific('en', () => {3})4import { languageSpecific } from 'storybook-root'5languageSpecific('en', () => {6})7import { languageSpecific } from 'storybook-root'8languageSpecific('en', () => {9})10import { languageSpecific } from 'storybook-root'11languageSpecific('en', () => {12})13import { languageSpecific } from 'storybook-root'14languageSpecific('en', () => {15})16import { languageSpecific } from 'storybook-root'17languageSpecific('en', () => {18})19import { languageSpecific } from 'storybook-root'20languageSpecific('en', () => {21})22import { languageSpecific } from 'storybook-root'23languageSpecific('en', () => {24})25import { language
Using AI Code Generation
1const { languageSpecific } = require("storybook-root");2const { en, fr } = require("./languages");3const language = languageSpecific({ en, fr });4const { languageSpecific } = require("storybook-root");5const { en, fr } = require("./languages");6const language = languageSpecific({ en, fr });7module.exports = {8 en: {9 },10 fr: {11 }12};13module.exports = {14 en: {15 },16 fr: {17 }18};19const { languageSpecific } = require("storybook-root");20const { en, fr } = require("./languages");21const language = languageSpecific({ en, fr });22const { languageSpecific } = require("storybook-root");23const { en, fr } = require("./languages");24const language = languageSpecific({ en, fr });25module.exports = {26 en: {27 },28 fr: {29 }30};31module.exports = {32 en: {33 },34 fr: {35 }36};37const { languageSpecific } = require("storybook-root");38const { en, fr } = require("./languages");39const language = languageSpecific({ en, fr });40const { languageSpecific } = require("storybook-root");41const { en,
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!!