Opensource JavaScript for space-separated values in websites or Node.js
<script src="ssv.js"></script>
npm install ssv --save
const ssv = require("ssv")
ssv.and
ssv.any
ssv.at
ssv.blank
ssv.count
ssv.edit
ssv.gum
ssv.jam
ssv.not
ssv.or
ssv.say
ssv.split
ssv.xor
ssv.yolo
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
- Get unique values present both in
left
andright
- 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"
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
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) // ""
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 SSV values
ssv.count(SSV="")
ssv.count("matt matt matt") // 3
ssv.count("mark tom ") // 2
- 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
ifboss
is string ssv.state
usesssv.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
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"
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() // ""
- Get values in
left
that are not inright
- 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"
- Get unique values found in
left
orright
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"
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
orssv.state
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() // []
- Get unique compact SSV string from state object or string
- Shorthand for
ssv.edit("", state)
- Like
ssv.yolo(state)
if string - Optimal for conditional CSS classes
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(" "))
Get unique values found in either left
or right
but not both
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", "matt tom ") // "mark 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"
- 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"