How to use kashe - 5 common examples

To help you get started, we’ve selected a few kashe examples, based on popular ways it is used in public projects.

Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.

github theKashey / used-styles / src / getCSS.ts View on Github external
import {kashe} from 'kashe';
import {StyleDefinition, UsedTypes, UsedTypesRef} from "./types";
import {extractUnmatchable, fromAst, getUnmatchableRules} from "./parser/fromAst";
import {assertIsReady, getStylesInText} from "./utils";

export const getUnusableStyles = kashe((def: StyleDefinition): UsedTypesRef => (
  Object
    .keys(def.ast || {})
    .filter(key => getUnmatchableRules(def.ast[key]).length > 0)
    .reduce((acc, file) => {
      acc[file] = true;
      return acc;
    }, {} as UsedTypesRef)
));

export const astToUsedStyles = kashe((styles: string[], def: StyleDefinition) => {
  const {lookup, ast} = def;
  const fetches: Record> = {};
  const visitedStyles = new Set();
  styles.forEach(className => {
    if (visitedStyles.has(className)) {
      return;
    }
    visitedStyles.add(className);

    const classes = className.split(' ');

    classes.forEach(singleClass => {
      const files = lookup[singleClass];
      if (files) {
        files.forEach(file => {
          if (!fetches[file]) {
github theKashey / used-styles / src / getCSS.ts View on Github external
const {fetches, usage} = astToUsedStyles(styles, def);

  return (
    usage
      .map(file => fromAst(Object.keys(fetches[file]), ast[file], filter))
      .join('\n')
  );
});

export const wrapInStyle = (styles: string) => (
  styles
    ? `<style data-used-styles="true" type="text/css">${styles}</style>`
    : ''
);

export const extractAllUnmatchable = kashe((def: StyleDefinition) =&gt; (
  Object
    .keys(def.ast || {})
    .reduce((acc, file) =&gt; acc + extractUnmatchable(def.ast[file]), '')
));

export const criticalStylesToString = (str: string, def: StyleDefinition, filter?: (selector: string) =&gt; boolean): string =&gt; {
  assertIsReady(def);
  return astToStyles(getStylesInText(str), def, filter);
};

export const getCriticalRules = (str: string, def: StyleDefinition, filter?: (selector: string) =&gt; boolean): string =&gt; {
  assertIsReady(def);
  return extractAllUnmatchable(def) + astToStyles(getStylesInText(str), def, filter);
};

export const getCriticalStyles = (str: string, def: StyleDefinition, filter?: (selector: string) =&gt; boolean): string =&gt; {
github theKashey / used-styles / src / getCSS.ts View on Github external
import {kashe} from 'kashe';
import {StyleDefinition, UsedTypes, UsedTypesRef} from "./types";
import {extractUnmatchable, fromAst, getUnmatchableRules} from "./parser/fromAst";
import {assertIsReady, getStylesInText} from "./utils";

export const getUnusableStyles = kashe((def: StyleDefinition): UsedTypesRef =&gt; (
  Object
    .keys(def.ast || {})
    .filter(key =&gt; getUnmatchableRules(def.ast[key]).length &gt; 0)
    .reduce((acc, file) =&gt; {
      acc[file] = true;
      return acc;
    }, {} as UsedTypesRef)
));

export const astToUsedStyles = kashe((styles: string[], def: StyleDefinition) =&gt; {
  const {lookup, ast} = def;
  const fetches: Record&gt; = {};
  const visitedStyles = new Set();
  styles.forEach(className =&gt; {
    if (visitedStyles.has(className)) {
      return;
github theKashey / used-styles / src / parser / fromAst.ts View on Github external
const prefix: string[] = [];
  const postfix: string[] = [];

  media.forEach(media =&gt; {
    prefix.push(`@media ${media} {`);
    postfix.push('}');
  });
  return [
    prefix.join(separator),
    postfix.join(separator),
  ]
};

type SelectorLookUp = Set;

const renderRule = kashe((rule: StyleSelector, style: StyleBody) =&gt; (
  `${rule.selector} { ${declsToString(style.rules)} }`
));

const isMatching = (rule: StyleSelector, rules: SelectorLookUp) =&gt; (
  rule.pieces.length &gt; 0 &amp;&amp;
  rule.pieces.every(piece =&gt; rules.has(piece))
);

const findMatchingSelectors = (rules: SelectorLookUp, selectors: StyleSelector[]): StyleSelector[] =&gt; (
  selectors.filter(rule =&gt; isMatching(rule, rules))
);

const findUnmatchableSelectors = (selectors: StyleSelector[]): StyleSelector[] =&gt; (
  selectors.filter(rule =&gt; rule.pieces.length === 0)
);
github theKashey / used-styles / src / getCSS.ts View on Github external
}, {})
  };

  return Object.keys(
    Object
      .keys(def.ast)
      .reduce((acc, file) =&gt; {
        if (flags[file]) {
          acc[file] = true
        }
        return acc;
      }, {})
  );
};

export const astToStyles = kashe((styles: string[], def: StyleDefinition, filter?: (selector: string) =&gt; boolean): string =&gt; {
  const {ast} = def;
  const {fetches, usage} = astToUsedStyles(styles, def);

  return (
    usage
      .map(file =&gt; fromAst(Object.keys(fetches[file]), ast[file], filter))
      .join('\n')
  );
});

export const wrapInStyle = (styles: string) =&gt; (
  styles
    ? `<style data-used-styles="true" type="text/css">${styles}</style>`
    : ''
);

kashe

Stateless weak memoization replacement for reselect and memoize-one

MIT
Latest version published 4 months ago

Package Health Score

67 / 100
Full package analysis

Popular kashe functions