/ssv

Space Separated Values. JavaScript library for spaced data. Fun and fast for classnames and beyond :two_hearts:

Primary LanguageJavaScriptUniversal Permissive License v1.0UPL-1.0

ssv

<script src="ssv.js"></script>
npm install ssv
const ssv = require("ssv")

methods

all

Test if SSV contains all search values

ssv.all(SSV="", search="")
ssv.all("mark tom travis", "scott") // false
ssv.all("mark tom travis", "mark tom") // true
ssv.all("mark tom travis", "mark scott") // false

and

  • Get unique values present both in left and right
  • Ideal for intersecting
ssv.and(left="", right="")
ssv.and("", "mark") // ""
ssv.and("mark matt travis", "tom scott") // ""
ssv.and("mark tom tom", "mark tom travis") // "mark tom"
let many = ["mark tom", "mark travis", "mark matt"]
many.reduce(ssv.and) // "mark"

any

Test if SSV contains any search values

ssv.any(SSV="", search="")
ssv.any("mark tom travis", "matt") // false
ssv.any("mark tom travis", "mark") // true
ssv.any("mark tom travis", "mark scott") // true
ssv.any("mark tom travis", "mark travis") // true

at

Get the value at index

ssv.at(SSV="", index)
ssv.at("mark tom travis", 0) // "mark"
ssv.at("mark tom travis", 1) // "tom"
ssv.at("mark tom travis", -1) // "travis"
ssv.at("mark tom travis", -2) // "tom"
ssv.at("mark tom travis", 5) // ""

blank

Test if SSV is blank

ssv.blank(SSV="")
ssv.blank("travis") // false
ssv.blank("      ") // true
ssv.blank("") // true
ssv.blank(0) // false
ssv.blank() // true

count

Count SSV values

ssv.count(SSV="")
ssv.count("matt matt matt") // 3
ssv.count("mark      tom ") // 2

edit

  • Edit an SSV string via an object or string
  • Optimal for batch editing CSS classes
  • Keys for falsey values are removed
  • Keys for truthy values are added
  • Removals process before adds
  • Result is compact and unique
  • Like ssv.or if boss is string
  • ssv.state uses ssv.edit
ssv.edit(SSV="", boss={})
ssv.edit("mark tom travis", {
  "matt": true,
  "tom scott": false
}) // "mark travis matt"

ssv.edit("mark", {
  "mark travis": true,
  "travis": false
}) // "mark travis"
ssv.edit("mark tom scott", Object.assign(
  { scott: false },
  { travis: true }
)) // "mark tom travis"
let bosses = [/* objects or strings */]
bosses.reduce(ssv.edit, "") // forward
bosses.reduceRight(ssv.edit, "") // backward

gum

Concatenate with compact space

ssv.gum(left="", right="")
ssv.gum("mark tom", "scott travis") // "mark tom scott travis"
ssv.gum("mark tom", "tom   travis") // "mark tom tom travis"
let many = ["tom tom", null, "travis travis", ""]
many.reduce(ssv.gum) // "tom tom travis travis"

jam

Compact excess space

ssv.jam(SSV)
ssv.jam("  mark  travis   matt ") // "mark travis matt"
ssv.jam("  matt      ") // "matt"
ssv.jam("  0     182 ") // "0 182"
ssv.jam(-182) // "-182"
ssv.jam(182) // "182"
ssv.jam(" ") // ""
ssv.jam(0) // "0"
ssv.jam() // ""

not

  • Get values in left that are not in right
  • Ideal for removing values or diffing
ssv.not(left="", right="")
ssv.not("mark tom travis", "tom") // "mark travis"
ssv.not("mark tom tom", "mark matt") // "tom tom"
ssv.not("matt matt matt", "") // "matt matt matt"
ssv.not("mark mark", "tom tom") // "mark mark"
ssv.not("mark tom tom tom", "tom") // "mark"
let many = ["mark tom", "mark travis", "mark matt"]
many.reduce(ssv.not) // "tom"

or

  • Get unique values found in left or right or both
  • Ideal for adding values or unioning
ssv.or(left="", right="")
ssv.or("mark tom ", "travis tom") // "mark tom travis"
ssv.or("mark tom tom", "travis tom") // "mark tom travis"
ssv.or("matt mark", "matt") // "matt mark"
let many = ["mark tom", "mark travis", "mark matt"]
many.reduce(ssv.or) // "mark tom travis matt"

say

Simply stringify unknown

ssv.say(unknown="")
ssv.say(undefined) // ""
ssv.say(null) // ""
ssv.say(0) // "0"
ssv.say(NaN) // "NaN"
ssv.say(182) // "182"
ssv.say("tom") // "tom"
ssv.say(true) // "true"
ssv.say(false) // "false"
ssv.say(new Number(182), "182")
ssv.say(new String("tom"), "tom")
ssv.say(new Boolean(true), "true")
  • Used internally when expecting strings
  • Not intended for arrays or plain objects
  • Join arrays instead like [].join(" ")
  • Plain objects may use ssv.edit or ssv.state

split

Split SSV into dense array

ssv.split(SSV)
ssv.split("mark tom travis") // ["mark", "tom", "travis"]
ssv.split("     mark  tom ") // ["mark", "tom"]
ssv.split("0 0  182       ") // ["0", "0", "182"]
ssv.split(0) // ["0"]
ssv.split(" ") // []
ssv.split("") // []
ssv.split() // []

state

ssv.state(state={})
ssv.state("mark  tom  ") // "mark tom"
ssv.state({
  "mark travis": true,
  "matt": true,
  "tom scott": false
}) // "mark travis matt"

ssv.state({
  "mark": true,
  "mark travis": true,
  "travis": false
}) // "mark mark travis"
let states = [/* objects or strings */]
ssv.yolo(states.map(ssv.state).join(" "))

xor

  • Get unique values found in either left or right but not both
  • Ideal for toggling values
ssv.xor(left="", right="")
ssv.xor("", "mark mark") // "mark"
ssv.xor("mark tom", "mark") // "tom"
ssv.xor("mark tom", "travis") // "mark tom travis"
ssv.xor("mark tom", "travis tom") // "mark travis"
ssv.xor("mark tom travis", "tom matt") // "mark travis matt"
ssv.xor("mark tom tom", "mark mark") // "tom"
ssv.xor("mark mark", "tom tom") // "mark tom"
let many = ["mark tom", "mark travis", "mark matt"]
many.reduce(ssv.xor) // "tom travis mark matt"
many.reduceRight(ssv.xor) // "matt travis mark tom"

yolo

  • Get unique SSV values
  • Hella fast unique loop
  • Be unique because yolo
  • Case sensitive
ssv.yolo(SSV="")
ssv.yolo("tom tom travis") // "tom travis"
ssv.yolo("Na na na na   ") // "Na na"
ssv.yolo("Na na na na".toLowerCase()) // "na"