import { replace, when, isNil, complement, map, length, both, toPairs, isEmpty, compose, AnyFunc, ifElse, identity, prop, typeIs } from 'pepka' export const emptyObject = Object.freeze({}) export const types = Object.freeze({ f: 'function', o: 'object', s: 'string' }) export const camelify = (str: string) => str.replace(/-(\w)/gu, (_s, l) => l.toUpperCase()) export const splitNonEscaped = (delims: string[]) => (str: string): string[] => { const delims_lns: number[] = map(length as (d: string) => number, delims) const out: string[] = [] let i: number, j: number, last_index = 0, delims_count = delims.length, str_len = str.length for(i=0; i { const patternRE = /url\(.*?\)/g const signsRE = /[,:;]/g return (v: string): string => v.replace(patternRE, (v) => v.replace(signsRE, (s) => `\\${s}`) ) })() export const unescape = when( complement(isNil), replace(/([^\\])\\([^\\])/g, '$1$2') ) export const valuable = both( complement(isEmpty), complement(isNil) ) export const join = (strings: string[], values: any[]) => strings.reduce((accum, str, i) => accum + str + (values.length>i ? values[i] : '') , '') export const isObject = typeIs('Object') export const isFunction = typeIs('Function') export const isWindow = typeIs('Window') export const isBrowser: boolean = (() => { try { return isWindow(window) } catch { return false } })() const tryUnwrap = map(ifElse( isFunction, identity, prop('default') )) type PluginArgsMap = {[i: number]: any[]} const callWith = ( args: PluginArgsMap, i: number, fn: AnyFunc ) => fn(...(args[i] || [])) export const preparePlugins = ( plugins: (AnyFunc | {default: AnyFunc})[], args: PluginArgsMap ) => compose( map(([i, p]) => callWith(args, i, p)), toPairs, tryUnwrap )(plugins) export const re = { comment: /((\s+?\/\/.*$)|\/\*(.|[\n\r])*?\*\/)/gm, trailing_ws: /(^|\r|\n)+[\t ]+/g, repeatingSeps: /([;\s]+|\s{2,})/g, trailingSeps: /(?:(}|{|]|)^[;\n\r ]+)|(?:[;\n\r ]+($|}|{|]))/g, rule: /^([\w-]+)(: *| +)(.*)$/, rule_free: /(^|\r|\n|;|{)\s*([a-z-]+)[ :][\t ]*?:?[\t ]*?([^;\r\n]+)/g, selector: /^(([\|~\$@>\*\.:&\(\)\^="\-\[\]]+).*[ ,]*)+:?$/, spread: /^\.\.\.(\S*)$/, media: /^@media /, delim: /\s*,\s*/g, trailing_colon: /(.*):$/, class_mod: /[.&]/, eol: /[\n\r]/g, tliterals: /css\`((.|\s)*?)\`/g, interp: /\${(?:[^{}]|{(?:[^{}]|{[^{}]*})*})*}/g }