I ran into the validFor field in SalesForce.

This post really helped explain it, but only left me with Java. Here’s a quick TS version.

const padLeft = (padding: string, length: number, x: string): string => {
  while (x.length < length) {
    x = `${padding}${x}`
  }
  return x
}

const toBinary = (number: number): string => padLeft('0', 8, number.toString(2))

// Converts a Base64 string into binary string.
// wAAA -> '110000000000000000000000'
// AAAI -> '000000000000000000001000'
const decodeBase64Binary = (value: string): string =>
  Buffer.from(value, 'base64')
  .reduce((acc, byte) => `${acc}${toBinary(byte)}`, '')

const decodeValidFor = (validFor: string) => {
  const bits = decodeBase64Binary(validFor)
  console.log({ validFor, bits})
}