ssv
v4.1.1
Published
space separated values module
Downloads
50
Maintainers
Readme
ssv
- Opensource JavaScript for space separated values
- Uses simple methods you can explore online
- Coded universally for websites or Node.js
- Compresses
<600B
- Fully tested
download
<script src="ssv.js"></script>
package
npm install ssv
const ssv = require("ssv")
methods
ssv.all
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.state
ssv.xor
ssv.yolo
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
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"
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
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
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 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"
or
- 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"
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
orssv.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
- 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(" "))
xor
- Get unique values found in either
left
orright
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"