Skip to content

Instantly share code, notes, and snippets.

@miyaokamarina
Last active August 16, 2019 07:40
Show Gist options
  • Save miyaokamarina/69f70e0f2d4d07286d933db69828df4b to your computer and use it in GitHub Desktop.
Save miyaokamarina/69f70e0f2d4d07286d933db69828df4b to your computer and use it in GitHub Desktop.
Classes list/dict reducer for TypeScript.

classlist.ts

Reduces list of anything that looks like classname to single space-separated string.

NB: Allows /[\w-]/ only in classnames. Classnames with other characters will be excluded from resulting list.

Requirements

  • Symbol.prototype.description (optional),
  • Reflect (optional).

If you don’t use symbols (lolol) or numbers (lol), you can use -no-symbols or -no-numbers.

Usage

classlist() //           ''
classlist(null) //       ''
classlist('') //         ''
classlist('ты пидор') // ''

classlist(1488) //  '1488'
classlist(1488n) // '1488n'

classlist('ti pidor') //                               'ti pidor'
classlist('ti.pidor') //                               'ti pidor'
classlist(Symbol('ti.pidor')) //                       'ti pidor'
classlist(['ti', 'pidor']) //                          'ti pidor'
classlist({ ti: 'pidor' }) //                          'ti pidor'
classlist({ ti: true, nyasha: false, pidor: true }) // 'ti pidor'
classlist({ ti: { pidor: true } }) //                  'ti pidor'

License

DON’T BE A DICK PUBLIC LICENSE

Version 1, July 2019

Copyright © 2019 Marina Miyaoka miyaokamarina@gmail.com

  1. Don’t be a dick.
  2. If you are, get in the damn robot.

THE SOFTWARE IS PROVIDED “AS IS”, WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

type Cls = string | boolean | null | undefined | void | ClsObject | ClsArray
type ClsObject = { readonly [k in keyof any]: Cls }
interface ClsArray extends ReadonlyArray<Cls> {}
declare interface ArrayConstructor {
isArray(c: Cls): c is ClsArray
}
const string = (visited: Set<Cls>, flat: string[], c: string): void => {
for (const s of c.split(/[\s.]+/)) {
if (!s || /[^\w-]/u.test(s)) continue
if (visited.has(s)) continue
else visited.add(s)
flat.push(s)
}
}
const object = <o extends ClsObject>(visited: Set<Cls>, flat: string[], c: o): void => {
const keys = Object.keys(c)
for (const k of keys) {
const v = c[k]
if (v) string(visited, flat, k)
if (typeof v === 'string') string(visited, flat, v)
else if (v) flatten(visited, flat, [v])
}
}
const flatten = (visited: Set<Cls>, flat: string[], cs: readonly Cls[]): readonly string[] => {
for (const c of cs) {
if (typeof c === 'string') {
string(visited, flat, c)
} else {
if (visited.has(c)) continue
else visited.add(c)
if (Array.isArray(c)) flatten(visited, flat, c)
else if (typeof c === 'object' && c) object(visited, flat, c)
}
}
return flat
}
const classlist = (...cs: readonly Cls[]) => flatten(new Set(), [], cs).join(' ')
type Cls = string | bigint | number | boolean | null | undefined | void | ClsObject | ClsArray
type ClsObject = { readonly [k in keyof any]: Cls }
interface ClsArray extends ReadonlyArray<Cls> {}
declare interface ArrayConstructor {
isArray(c: Cls): c is ClsArray
}
const isPrimitive = (c: Cls): c is string | bigint | number => {
switch (typeof c) {
case 'string':
case 'bigint':
case 'number':
return true
default:
return false
}
}
const stringify = (c: symbol | bigint | number) => typeof c === 'bigint' ? `${String(c)}n` : String(c)
const primitive = (visited: Set<Cls>, flat: string[], c: string | bigint | number): void => {
if (typeof c !== 'string') primitive(visited, flat, stringify(c))
else
for (const s of c.split(/[\s.]+/)) {
if (!s || /[^\w-]/u.test(s)) continue
if (visited.has(s)) continue
else visited.add(s)
flat.push(s)
}
}
const object = <o extends ClsObject>(visited: Set<Cls>, flat: string[], c: o): void => {
const keys = Object.keys(c)
for (const k of keys) {
const v = c[k]
if (v || v === 0) primitive(visited, flat, k)
if (isPrimitive(v)) primitive(visited, flat, v)
else if (v) flatten(visited, flat, [v])
}
}
const flatten = (visited: Set<Cls>, flat: string[], cs: readonly Cls[]): readonly string[] => {
for (const c of cs) {
if (isPrimitive(c)) {
primitive(visited, flat, c)
} else {
if (visited.has(c)) continue
else visited.add(c)
if (Array.isArray(c)) flatten(visited, flat, c)
else if (typeof c === 'object' && c) object(visited, flat, c)
}
}
return flat
}
const classlist = (...cs: readonly Cls[]) => flatten(new Set(), [], cs).join(' ')
type Cls = string | symbol | bigint | number | boolean | null | undefined | void | ClsObject | ClsArray
type ClsObject = { readonly [k in keyof any]: Cls }
interface ClsArray extends ReadonlyArray<Cls> {}
declare interface ArrayConstructor {
isArray(c: Cls): c is ClsArray
}
const isPrimitive = (c: Cls): c is string | symbol | bigint | number => {
switch (typeof c) {
case 'string':
case 'symbol':
case 'bigint':
case 'number':
return true
default:
return false
}
}
const stringify = (c: symbol | bigint | number) => {
if (typeof c === 'symbol') return c.description || ''
if (typeof c === 'bigint') return `${String(c)}n`
return String(c)
}
const primitive = (visited: Set<Cls>, flat: string[], c: string | symbol | bigint | number): void => {
if (typeof c !== 'string') primitive(visited, flat, stringify(c))
else
for (const s of c.split(/[\s.]+/)) {
if (!s || /[^\w-]/u.test(s)) continue
if (visited.has(s)) continue
else visited.add(s)
flat.push(s)
}
}
const object = <o extends ClsObject>(visited: Set<Cls>, flat: string[], c: o): void => {
const keys = Reflect.ownKeys(c) as readonly (keyof o)[]
for (const k of keys) {
const v = c[k]
if (v || v === 0) primitive(visited, flat, k)
if (isPrimitive(v)) primitive(visited, flat, v)
else if (v) flatten(visited, flat, [v])
}
}
const flatten = (visited: Set<Cls>, flat: string[], cs: readonly Cls[]): readonly string[] => {
for (const c of cs) {
if (isPrimitive(c)) {
primitive(visited, flat, c)
} else {
if (visited.has(c)) continue
else visited.add(c)
if (Array.isArray(c)) flatten(visited, flat, c)
else if (typeof c === 'object' && c) object(visited, flat, c)
}
}
return flat
}
const classlist = (...cs: readonly Cls[]) => flatten(new Set(), [], cs).join(' ')
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment