Skip to content

Instantly share code, notes, and snippets.

@barneycarroll
Created January 16, 2018 15:00
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save barneycarroll/6e2dc40f513890914f162475fd8af0be to your computer and use it in GitHub Desktop.
Save barneycarroll/6e2dc40f513890914f162475fd8af0be to your computer and use it in GitHub Desktop.
I'm afraaaid that that is the nature of the Hard Left
export const censored = $ =>
$ == undefined || $ === false ? '' : $
export const censor = ({ censored, strings, expressions }) =>
strings.reduce((a, c, i, [], b = expressions[i - 1]) =>
a + censored(b) ? '' : b + c
)
export default (strings, ...expressions) =>
Array.isArray(strings)
?
censor({expressions, censored, strings})
:
(censored, ...expressions) =>
censor({
expressions,
censored: strings,
strings: censored,
})

ThinkPol

Censor expressions in tagged template literals, the same way templates throw out some falsey values. It's very common in string constructrion to want to be able to discard undefined or false evaluating expressions, in order to enable eg ${ x && intl(x) } - thus avoiding ternary expressions whose last operand is always an empty string '' (${ x ? intl(x) : '' }). ThinkPol makes this easier by BANNING ALL THOUGHTS IT DOESN'T LIKE BECAUSE IT BELIEVES ITS SUPERIOR TO YOU.

API

// Convenience overloaded API
import censor from 'thinkpol'

// Default censoring:
censor`
  0         : ${ 0 }
  NaN       : ${ NaN }
  null      : ${ null }
  false     : ${ false }
  undefined : ${ undefined }
`

// Custom censoring:
censor(x => !x)` // Anything falsey? Fair enough
  blah ${ 
  foo 
} blah ${ 
  bar 
} blah
`

// Explicit API:
import { censored, censor } from 'thinkpol'

const falsies = [0, NaN, null, false, undefined]

censor({
  censored,
  strings: falsies.map(String),
  expressions: falsies,
})

censor({
  censored: x => !x),
  strings: ['\nblah ', ' blah ', ' blah\n'],
  expressions: [foo, bar],

Default censorship

By default ThinkPol will replace all expressions evaluating to undefined, null or false with an empty string.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment