Skip to content

Commit 9f9c3ff

Browse files
committed
init commit
0 parents  commit 9f9c3ff

9 files changed

Lines changed: 231 additions & 0 deletions

File tree

.gitignore

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
# Logs
2+
logs
3+
*.log
4+
npm-debug.log*
5+
6+
# Runtime data
7+
pids
8+
*.pid
9+
*.seed
10+
11+
# Directory for instrumented libs generated by jscoverage/JSCover
12+
lib-cov
13+
14+
# Coverage directory used by tools like istanbul
15+
coverage
16+
17+
# Grunt intermediate storage (http://gruntjs.com/creating-plugins#storing-task-files)
18+
.grunt
19+
20+
# node-waf configuration
21+
.lock-wscript
22+
23+
# Compiled binary addons (http://nodejs.org/api/addons.html)
24+
build/Release
25+
26+
# Dependency directory
27+
# https://docs.npmjs.com/misc/faq#should-i-check-my-node-modules-folder-into-git
28+
node_modules
29+
30+
# Optional npm cache directory
31+
.npm
32+
33+
# Optional REPL history
34+
.node_repl_history

.travis.yml

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
language: node_js
2+
node_js:
3+
- "4.1"
4+
- "4.0"
5+
- "0.12"
6+
- "0.11"
7+
- "0.10"
8+
- "iojs"
9+
branches:
10+
only:
11+
- master

LICENSE

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
The MIT License (MIT)
2+
3+
Copyright (c) 2016 Isis T. Baulig
4+
5+
Permission is hereby granted, free of charge, to any person obtaining a copy
6+
of this software and associated documentation files (the "Software"), to deal
7+
in the Software without restriction, including without limitation the rights
8+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9+
copies of the Software, and to permit persons to whom the Software is
10+
furnished to do so, subject to the following conditions:
11+
12+
The above copyright notice and this permission notice shall be included in all
13+
copies or substantial portions of the Software.
14+
15+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21+
SOFTWARE.

README.md

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
# Flat Insert
2+
[![Build Status](https://travis-ci.org/grindcode/flat-insert.svg)](https://travis-ci.org/grindcode/flat-insert)
3+
Returns a new _Array_ or _String_ after inserting and flatten `value` at `index`.
4+
5+
[![browser support](https://ci.testling.com/grindcode/flat-insert.png)
6+
](https://ci.testling.com/grindcode/flat-insert)
7+
8+
## Get Started
9+
```
10+
npm install flat-insert
11+
```
12+
13+
## API
14+
### insert(input, value, [index])
15+
Returns new `string` or `array` depending on `input` type.
16+
* `input`: Input value to be modified. (**String**|**Array**)
17+
* `value`: Any value to be inserted into `input`. (**mixed**)
18+
* If `input` type is _String_, `value` will be turned into _String_.
19+
* If `input` and `value` types are _Array_, then `value` will be flatten.
20+
* `index`: Index or position at `input` where insert `value`. (**Number**; default: `0`)
21+
22+
### Usage
23+
```
24+
var insert = require('flat-insert')
25+
26+
insert([1, 2, 3], [1.1, 1.2], 1)
27+
// → '[1, 1.1, 1.2, 2, 3]'
28+
29+
insert([1, 2, 3], 'meh', 1)
30+
// → '[1, 'meh', 2, 3]'
31+
32+
insert('123', 'yo', 2)
33+
// → '12yo3'
34+
```
35+
36+
## License
37+
See the [License](LICENSE) file.

index.js

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
module.exports = function (res, val, i) {
2+
return res.slice(0, i || 0).concat(val).concat(res.slice(i || 0))
3+
}

package.json

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
{
2+
"name": "flat-insert",
3+
"version": "0.1.0",
4+
"description": "Returns a new Array or String after inserting and flatten value at index.",
5+
"main": "index.js",
6+
"scripts": {
7+
"test": "tape test/*.js"
8+
},
9+
"repository": {
10+
"type": "git",
11+
"url": "git+https://github.com/grindcode/flat-insert.git"
12+
},
13+
"keywords": [
14+
"array",
15+
"string",
16+
"insert",
17+
"inject",
18+
"position",
19+
"index",
20+
"join",
21+
"concat",
22+
"slice",
23+
"splice",
24+
"flat"
25+
],
26+
"author": "Isis T. Baulig",
27+
"license": "MIT",
28+
"bugs": {
29+
"url": "https://github.com/grindcode/flat-insert/issues"
30+
},
31+
"homepage": "https://github.com/grindcode/flat-insert#readme",
32+
"devDependencies": {
33+
"tape": "^4.4.0"
34+
}
35+
}

test/arrays.js

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
var test = require('tape')
2+
var inject = require('../index.js')
3+
4+
test('insert undefined into array', function (t) {
5+
t.plan(1)
6+
var result = inject([1, 2, 3], undefined, 1)
7+
t.deepEqual(result, [1, undefined, 2, 3])
8+
})
9+
10+
test('insert a Number into array', function (t) {
11+
t.plan(1)
12+
var result = inject([1, 2, 3], 4, 1)
13+
t.deepEqual(result, [1, 4, 2, 3])
14+
})
15+
16+
test('insert a String into array', function (t) {
17+
t.plan(1)
18+
var result = inject([1, 2, 3], 'yo', 2)
19+
t.deepEqual(result, [1, 2, 'yo', 3])
20+
})
21+
22+
test('insert an Array of mixed values into array', function (t) {
23+
t.plan(1)
24+
var result = inject([1, 2, 3], [4, true, 'yo'], 1)
25+
t.deepEqual(result, [1, 4, true, 'yo', 2, 3])
26+
})
27+
28+
test('insert duplicated values into array', function (t) {
29+
t.plan(1)
30+
var result = inject([1, 2, 3], [1, 2, 3], 1)
31+
t.deepEqual(result, [1, 1, 2, 3, 2, 3])
32+
})

test/indexes.js

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
var test = require('tape')
2+
var inject = require('../index.js')
3+
4+
test('index is undefined', function (t) {
5+
t.plan(1)
6+
var result = inject([1, 2, 3], 'yo', undefined)
7+
t.deepEqual(result, ['yo', 1, 2, 3])
8+
})
9+
10+
test('insert at offset index', function (t) {
11+
t.plan(1)
12+
var result = inject([1, 2, 3], 'last', 10)
13+
t.deepEqual(result, [1, 2, 3, 'last'])
14+
})
15+
16+
test('insert at index == 0', function (t) {
17+
t.plan(1)
18+
var result = inject([1, 2, 3], 'first', 0)
19+
t.deepEqual(result, ['first', 1, 2, 3])
20+
})
21+
22+
test('insert at negative index', function (t) {
23+
t.plan(1)
24+
var result = inject([1, 2, 3], 'before last', -1)
25+
t.deepEqual(result, [1, 2, 'before last', 3])
26+
})
27+
28+
test('insert at negative offset index', function (t) {
29+
t.plan(1)
30+
var result = inject([1, 2, 3], 'first', -10)
31+
t.deepEqual(result, ['first', 1, 2, 3])
32+
})

test/strings.js

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
var test = require('tape')
2+
var inject = require('../index.js')
3+
4+
test('insert undefined into string', function (t) {
5+
t.plan(1)
6+
var result = inject('123', undefined, 1)
7+
t.equal(result, '1undefined23')
8+
})
9+
10+
test('insert a Number into string', function (t) {
11+
t.plan(1)
12+
var result = inject('123', 4, 1)
13+
t.equal(result, '1423')
14+
})
15+
16+
test('insert a String into string', function (t) {
17+
t.plan(1)
18+
var result = inject('123', 'yo', 2)
19+
t.equal(result, '12yo3')
20+
})
21+
22+
test('insert an Array of mixed values into string', function (t) {
23+
t.plan(1)
24+
var result = inject('123', [4, true, 'yo'], 1)
25+
t.equal(result, '14,true,yo23')
26+
})

0 commit comments

Comments
 (0)