Skip to content

Commit

Permalink
Add slice method (#6)
Browse files Browse the repository at this point in the history
  • Loading branch information
ryanve authored Sep 8, 2017
1 parent 7baa4a2 commit 37f9456
Show file tree
Hide file tree
Showing 4 changed files with 29 additions and 0 deletions.
6 changes: 6 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,9 @@ ssv.add('mark travis', 'matt') // 'mark travis matt'
ssv.add('mark travis', 'travis') // 'mark travis'
ssv.push('mark travis', 'travis') // 'mark travis travis'
ssv.pop('mark travis tom') // 'tom'
ssv.slice('travis mark tom', 1) // 'mark tom'
ssv.slice('travis mark tom', -1) // 'tom'
ssv.slice(' travis mark tom', 0, 1) // 'travis'
ssv.concat('mark tom', 'travis matt') // 'mark tom travis matt'
ssv.concat(' mark tom ', ' travis matt ') // 'mark tom travis matt'
ssv.compact(' mark travis matt ') // 'mark travis matt'
Expand All @@ -48,6 +51,9 @@ Add <var>value</var> to <var>SSV</var> string whether unique or not
### ssv.pop(SSV, value)
Get last SSV value

### ssv.slice(SSV, begin, end)
Slice SSV values per `[].slice`

### ssv.compact(SSV)
Normalize <var>SSV</var> string to a trim compact string

Expand Down
1 change: 1 addition & 0 deletions ssv.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ declare module ssv {
export function concat(ssv: string, more: string): string;
export function parse(ssv: string): string[];
export function split(ssv: string): string[];
export function slice(ssv: string, begin?: number, end?: number): string;
}

declare module "ssv" {
Expand Down
9 changes: 9 additions & 0 deletions ssv.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
typeof module != 'undefined' && module.exports ? module.exports = make() : root[name] = make()
}(this, 'ssv', function() {

var vacant
var api = {}
var word = /\S+/g
var space = ' '
Expand Down Expand Up @@ -32,6 +33,13 @@
return compact(ssv + space + more)
}

function slice(ssv, begin, end) {
ssv = split(ssv)
if (vacant === begin) begin = 0
if (vacant === end) end = ssv.length
return ssv.slice(begin, end).join(space)
}

function push(ssv, value) {
ssv = split(ssv)
ssv.push(value)
Expand All @@ -55,5 +63,6 @@
api['push'] = push
api['add'] = add
api['remove'] = remove
api['slice'] = slice
return api
});
13 changes: 13 additions & 0 deletions test.js
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,19 @@
expect(api.has(' yes no ', 'yes'), true)
console.log('#has tests passed')

expect(api.slice(''), '')
expect(api.slice('', 5), '')
expect(api.slice('', -1), '')
expect(api.slice('zed one two', 0), 'zed one two')
expect(api.slice('zed one two', -2), 'one two')
expect(api.slice('zed one two', 0, 1), 'zed')
expect(api.slice('zed one two', 1, 2), 'one')
expect(api.slice('zed one two', 1, 3), 'one two')
expect(api.slice('zed one two', 0, -2), 'zed')
expect(api.slice('zed one two', 0, -1), 'zed one')
expect(api.slice('zed one two', 1, -1), 'one')
console.log('#slice tests passed')

expect(api.pop(''), '')
expect(api.pop('yes'), 'yes')
expect(api.pop('yes no'), 'no')
Expand Down

0 comments on commit 37f9456

Please sign in to comment.