All files / packages/design-tokens/src/style-dictionary/filters mobile-filter.js

100% Statements 30/30
100% Branches 20/20
100% Functions 11/11
100% Lines 26/26

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148                                            2x 7076x             2x 7075x             2x 7075x             2x 7075x             2x 7075x               3x           3x           4x           4x           2x 35371x               1x         1x         1x         1x         1x         1x         1x         1x         1x           1x          
/**
 * Mobile Token Filters
 *
 * Filter functions for categorizing tokens in mobile outputs (Android, iOS).
 * These filters are used at the file level in Style Dictionary configuration
 * to pre-filter tokens before they reach the format functions.
 */
 
import {
  PATH_INDEX,
  CATEGORY,
  NAMESPACE,
  SCOPE,
  isSystemColorToken,
  isReferenceColorToken,
  isSemanticColorToken,
  isPaletteToken,
} from '../utils/mobile-helpers.js';
 
/**
 * Filter for spacing tokens (slds.g.spacing.*)
 */
export const spacingFilterFn = (token) =>
  token.path[PATH_INDEX.NAMESPACE] === NAMESPACE.SLDS &&
  token.path[PATH_INDEX.SCOPE] === SCOPE.GLOBAL &&
  token.path[PATH_INDEX.CATEGORY] === CATEGORY.SPACING;
 
/**
 * Filter for sizing tokens (slds.g.sizing.*)
 */
export const sizingFilterFn = (token) =>
  token.path[PATH_INDEX.NAMESPACE] === NAMESPACE.SLDS &&
  token.path[PATH_INDEX.SCOPE] === SCOPE.GLOBAL &&
  token.path[PATH_INDEX.CATEGORY] === CATEGORY.SIZING;
 
/**
 * Filter for radius/shape tokens (slds.g.radius.*)
 */
export const radiusFilterFn = (token) =>
  token.path[PATH_INDEX.NAMESPACE] === NAMESPACE.SLDS &&
  token.path[PATH_INDEX.SCOPE] === SCOPE.GLOBAL &&
  token.path[PATH_INDEX.CATEGORY] === CATEGORY.RADIUS;
 
/**
 * Filter for duration/motion tokens (slds.g.duration.*)
 */
export const durationFilterFn = (token) =>
  token.path[PATH_INDEX.NAMESPACE] === NAMESPACE.SLDS &&
  token.path[PATH_INDEX.SCOPE] === SCOPE.GLOBAL &&
  token.path[PATH_INDEX.CATEGORY] === CATEGORY.DURATION;
 
/**
 * Filter for font tokens (slds.g.font.*)
 */
export const fontFilterFn = (token) =>
  token.path[PATH_INDEX.NAMESPACE] === NAMESPACE.SLDS &&
  token.path[PATH_INDEX.SCOPE] === SCOPE.GLOBAL &&
  token.path[PATH_INDEX.CATEGORY] === CATEGORY.FONT;
 
/**
 * Filter for system color tokens (slds.g.color.{family}.base.{scale})
 * Reuses existing helper function
 */
export const systemColorFilterFn = (token) => isSystemColorToken(token);
 
/**
 * Filter for reference color tokens (slds.r.color.*)
 * Reuses existing helper function
 */
export const referenceColorFilterFn = (token) => isReferenceColorToken(token);
 
/**
 * Filter for semantic color tokens (slds.g.color.{semantic-name})
 * Reuses existing helper function
 */
export const semanticColorFilterFn = (token) => isSemanticColorToken(token);
 
/**
 * Filter for palette color tokens (alias.palette.*)
 * Reuses existing helper function
 */
export const paletteColorFilterFn = (token) => isPaletteToken(token);
 
/**
 * Base mobile filter - includes all mobile-relevant tokens (global, reference, and palette aliases)
 * Used for TokensAll and color files that need cross-references
 */
export const mobileFilterFn = (token) =>
  (token.path[0] === 'slds' && (token.path[1] === 'g' || token.path[1] === 'r')) ||
  (token.path[0] === 'alias' && token.path[1] === 'palette');
 
/**
 * Register all mobile filters with Style Dictionary
 * @param {object} StyleDictionary - Style Dictionary instance
 */
export function registerMobileFilters(StyleDictionary) {
  StyleDictionary.registerFilter({
    name: 'filter/mobile-spacing',
    filter: spacingFilterFn,
  });
 
  StyleDictionary.registerFilter({
    name: 'filter/mobile-sizing',
    filter: sizingFilterFn,
  });
 
  StyleDictionary.registerFilter({
    name: 'filter/mobile-radius',
    filter: radiusFilterFn,
  });
 
  StyleDictionary.registerFilter({
    name: 'filter/mobile-duration',
    filter: durationFilterFn,
  });
 
  StyleDictionary.registerFilter({
    name: 'filter/mobile-font',
    filter: fontFilterFn,
  });
 
  StyleDictionary.registerFilter({
    name: 'filter/mobile-system-colors',
    filter: systemColorFilterFn,
  });
 
  StyleDictionary.registerFilter({
    name: 'filter/mobile-reference-colors',
    filter: referenceColorFilterFn,
  });
 
  StyleDictionary.registerFilter({
    name: 'filter/mobile-semantic-colors',
    filter: semanticColorFilterFn,
  });
 
  StyleDictionary.registerFilter({
    name: 'filter/mobile-palette-colors',
    filter: paletteColorFilterFn,
  });
 
  // Register the base mobile filter
  StyleDictionary.registerFilter({
    name: 'filter/mobile',
    filter: mobileFilterFn,
  });
}