-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
xiaowei.wzw
authored and
xiaowei.wzw
committed
Dec 8, 2017
0 parents
commit 2468aed
Showing
10 changed files
with
411 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
{ | ||
"presets": ["env"] | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
.project | ||
.settings | ||
.idea | ||
.DS_Store | ||
.nyc_output | ||
coverage | ||
node_modules |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
node_modules/ | ||
.nyc_output/ | ||
coverage/ | ||
tests/ | ||
.gitignore | ||
.travis.yml | ||
.babelrc |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
language: node_js | ||
node_js: | ||
- '4' | ||
- '4' | ||
- '5' | ||
- '6' | ||
- '7' | ||
- '8' | ||
script: "npm run test" | ||
# Send coverage data to Coveralls | ||
after_script: "cat ./coverage/lcov.info | ./node_modules/coveralls/bin/coveralls.js" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
MIT License | ||
|
||
Copyright (c) 2017 hustcc | ||
|
||
Permission is hereby granted, free of charge, to any person obtaining a copy | ||
of this software and associated documentation files (the "Software"), to deal | ||
in the Software without restriction, including without limitation the rights | ||
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell | ||
copies of the Software, and to permit persons to whom the Software is | ||
furnished to do so, subject to the following conditions: | ||
|
||
The above copyright notice and this permission notice shall be included in all | ||
copies or substantial portions of the Software. | ||
|
||
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | ||
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, | ||
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE | ||
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER | ||
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, | ||
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE | ||
SOFTWARE. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,56 @@ | ||
# nano-slice | ||
|
||
> Nano version for slice of string and array, just like Python. | ||
[![Build Status](https://travis-ci.org/NanoPackage/nano-slice.svg?branch=master)](https://travis-ci.org/NanoPackage/nano-slice) [![Coverage Status](https://coveralls.io/repos/github/NanoPackage/nano-slice/badge.svg?branch=master)](https://coveralls.io/github/NanoPackage/nano-slice?branch=master) [![npm](https://img.shields.io/npm/v/nano-slice.svg?style=flat-square)](https://www.npmjs.com/package/nano-slice) [![npm](https://img.shields.io/npm/dt/nano-slice.svg?style=flat-square)](https://www.npmjs.com/package/nano-slice) [![npm](https://img.shields.io/npm/l/nano-slice.svg?style=flat-square)](https://www.npmjs.com/package/nano-slice) | ||
|
||
|
||
# 1. Install | ||
|
||
> **npm install nano-slice** | ||
|
||
```js | ||
var slice = require('nano-slice'); | ||
|
||
//or | ||
|
||
import slice from 'nano-slice'; | ||
``` | ||
|
||
|
||
# 2. Usage | ||
|
||
There is only one API named `slice`. | ||
|
||
```js | ||
// for array | ||
const arr = slice([1, '2', 3, '4', 5, '6', 7, '8', 9, '0']); | ||
|
||
arr['2:5']; // [3, '4', 5] | ||
arr[':-2']; // [1, '2', 3, '4', 5, '6', 7, '8'] | ||
arr['-2:']; // [9, '0'] | ||
arr['1:5:2']; // ['2', '4'] | ||
arr['5:1:-2']; // ['6', '4'] | ||
|
||
// for string | ||
const str = slice('1234567890'); | ||
str['2:5']; // '345' | ||
str[':-2']; // '12345678' | ||
str['-2:']; // '90' | ||
str['1:5:2']; // '24' | ||
str['5:1:-2']; // '64' | ||
|
||
``` | ||
|
||
|
||
# 3. Test | ||
|
||
> npm install | ||
> | ||
> npm test | ||
|
||
# LICENSE | ||
|
||
MIT@[hustcc](https://github.com/hustcc). |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,80 @@ | ||
import slice from '../src/'; | ||
|
||
describe('nano-slice', () => { | ||
test('array', () => { | ||
const arr = [1, '2', 3, '4', 5, '6', 7, '8', 9, '0']; | ||
expect(slice(arr)[200]).toEqual([undefined]); | ||
|
||
expect(slice(arr)['2']).toEqual([3]); | ||
expect(slice(arr)[2]).toEqual([3]); | ||
|
||
expect(slice(arr)['0:3']).toEqual([1, '2', 3]); | ||
expect(slice(arr)['2:5']).toEqual([3, '4', 5]); | ||
|
||
expect(slice(arr)[':2']).toEqual([1, '2']); | ||
expect(slice(arr)[':-2']).toEqual([1, '2', 3, '4', 5, '6', 7, '8']); | ||
|
||
expect(slice(arr)['2:']).toEqual([3, '4', 5, '6', 7, '8', 9, '0']); | ||
expect(slice(arr)['-2:']).toEqual([9, '0']); | ||
|
||
expect(slice(arr)[':']).toEqual([1, '2', 3, '4', 5, '6', 7, '8', 9, '0']); | ||
|
||
expect(slice(arr)['::3']).toEqual([1, '4', 7, '0']); | ||
expect(slice(arr)['1:5:2']).toEqual(['2', '4']); | ||
|
||
expect(slice(arr)['-5:-1']).toEqual(['6', 7, '8', 9]); | ||
expect(slice(arr)['-5:-1:2']).toEqual(['6', '8']); | ||
expect(slice(arr)['5:1:-2']).toEqual(['6', '4']); | ||
expect(slice(arr)['-1:0:-1']).toEqual(['0', 9, '8', 7, '6', 5, '4', 3, '2']); | ||
|
||
expect(slice(arr)['-1:-5']).toEqual([]); | ||
expect(slice(arr)['-5:-1:-1']).toEqual([]); | ||
|
||
expect(slice(arr)[':20']).toEqual([1, '2', 3, '4', 5, '6', 7, '8', 9, '0']); | ||
}); | ||
test('string', () => { | ||
const str = '1234567890'; | ||
expect(slice(str)[200]).toEqual(''); | ||
|
||
expect(slice(str)['2']).toEqual('3'); | ||
expect(slice(str)[2]).toEqual('3'); | ||
|
||
expect(slice(str)['0:3']).toEqual('123'); | ||
expect(slice(str)['2:5']).toEqual('345'); | ||
|
||
expect(slice(str)[':2']).toEqual('12'); | ||
expect(slice(str)[':-2']).toEqual('12345678'); | ||
|
||
expect(slice(str)['2:']).toEqual('34567890'); | ||
expect(slice(str)['-2:']).toEqual('90'); | ||
|
||
expect(slice(str)[':']).toEqual('1234567890'); | ||
|
||
expect(slice(str)['::3']).toEqual('1470'); | ||
expect(slice(str)['1:5:2']).toEqual('24'); | ||
|
||
expect(slice(str)['-5:-1']).toEqual('6789'); | ||
expect(slice(str)['-5:-1:2']).toEqual('68'); | ||
expect(slice(str)['5:1:-2']).toEqual('64'); | ||
expect(slice(str)['-1:0:-1']).toEqual('098765432'); | ||
|
||
expect(slice(str)['-1:-5']).toEqual(''); | ||
expect(slice(str)['-5:-1:-1']).toEqual(''); | ||
|
||
expect(slice(str)[':20']).toEqual('1234567890'); | ||
}); | ||
|
||
test('exception', () => { | ||
expect(() => { | ||
slice('1234567890')['-1:9:0']; | ||
}).toThrow('Step can not be zero!'); | ||
expect(() => { | ||
slice(123)['-1:9:0']; | ||
}).toThrow('Only string and array can be sliced!'); | ||
}); | ||
|
||
test('monkey', () => { | ||
// TODO | ||
// random path to run. | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,94 @@ | ||
'use strict'; | ||
|
||
Object.defineProperty(exports, "__esModule", { | ||
value: true | ||
}); | ||
|
||
var _slicedToArray = function () { function sliceIterator(arr, i) { var _arr = []; var _n = true; var _d = false; var _e = undefined; try { for (var _i = arr[Symbol.iterator](), _s; !(_n = (_s = _i.next()).done); _n = true) { _arr.push(_s.value); if (i && _arr.length === i) break; } } catch (err) { _d = true; _e = err; } finally { try { if (!_n && _i["return"]) _i["return"](); } finally { if (_d) throw _e; } } return _arr; } return function (arr, i) { if (Array.isArray(arr)) { return arr; } else if (Symbol.iterator in Object(arr)) { return sliceIterator(arr, i); } else { throw new TypeError("Invalid attempt to destructure non-iterable instance"); } }; }(); | ||
|
||
/** | ||
* 抛出一个异常 | ||
* @param s | ||
*/ | ||
var invariant = function invariant(condition, s) { | ||
if (!condition) throw new Error(s); | ||
}; | ||
|
||
/** | ||
* 模仿 Python 的分片操作 | ||
* @param v | ||
* @param start | ||
* @param end | ||
* @param step | ||
* @returns {Array} | ||
*/ | ||
var slice = function slice(v, start, end, step) { | ||
var r = []; | ||
var i = void 0; | ||
if (step > 0) { | ||
for (i = start; i < end; i += step) { | ||
r.push(v[i]); | ||
} | ||
} else { | ||
for (i = start; i > end; i += step) { | ||
r.push(v[i]); | ||
} | ||
} | ||
return r; | ||
}; | ||
|
||
/** | ||
* parse a string / number to number. | ||
* | ||
* parsetInt('') === NaN | ||
* Number('') === 0 | ||
* @param n | ||
*/ | ||
var parseNumber = function parseNumber(n) { | ||
return isNaN(parseInt(n)) ? NaN : Number(n); | ||
}; | ||
|
||
/** | ||
* slice entry 方法 | ||
* @param v | ||
* @returns {Proxy} | ||
*/ | ||
|
||
exports.default = function (v) { | ||
// 校验输入必须为字符串或者数组 | ||
invariant(typeof v === 'string' || Array.isArray(v), 'Only string and array can be sliced!'); | ||
|
||
return new Proxy({}, { | ||
get: function get(_, path) { | ||
var r = void 0, | ||
l = v.length; | ||
// 如果直接为数字,那么直接返回 | ||
if (!isNaN(parseNumber(path))) { | ||
r = [v[path]]; | ||
} else { | ||
var _path$split$map = path.split(':').map(function (s) { | ||
return parseNumber(s); | ||
}), | ||
_path$split$map2 = _slicedToArray(_path$split$map, 3), | ||
start = _path$split$map2[0], | ||
end = _path$split$map2[1], | ||
step = _path$split$map2[2]; | ||
// 异常的时候默认值 | ||
|
||
|
||
start = isNaN(start) ? 0 : start < 0 ? l + start : start; | ||
|
||
end = isNaN(end) ? l : end < 0 ? l + end : // 小于 0 转成正数 | ||
end > l ? l : end; // 最大为长度 | ||
|
||
step = isNaN(step) ? 1 : step; | ||
|
||
invariant(step !== 0, 'Step can not be zero!'); | ||
|
||
r = slice(v, start, end, step); | ||
} | ||
// return | ||
return Array.isArray(v) ? r : r.join(''); | ||
} | ||
}); | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,56 @@ | ||
{ | ||
"name": "nano-slice", | ||
"version": "1.0.0", | ||
"description": "nano version for slice of string and array like Python.", | ||
"main": "lib/index.js", | ||
"scripts": { | ||
"build": "babel src --out-dir lib", | ||
"test": "jest" | ||
}, | ||
"repository": { | ||
"type": "git", | ||
"url": "git+https://github.com/NanoPackage/nano-slice.git" | ||
}, | ||
"keywords": [ | ||
"nano", | ||
"nano-slice", | ||
"nano-package" | ||
], | ||
"author": "hustcc", | ||
"license": "MIT", | ||
"bugs": { | ||
"url": "https://github.com/NanoPackage/nano-slice/issues" | ||
}, | ||
"homepage": "https://github.com/NanoPackage/nano-slice#readme", | ||
"devDependencies": { | ||
"babel-cli": "^6.26.0", | ||
"babel-core": "^6.26.0", | ||
"babel-jest": "^21.2.0", | ||
"babel-preset-env": "^1.6.1", | ||
"coveralls": "^3.0.0", | ||
"jest": "^21.2.1" | ||
}, | ||
"jest": { | ||
"testRegex": "(/__tests__/index)\\.(ts|tsx|js)$", | ||
"moduleFileExtensions": [ | ||
"ts", | ||
"tsx", | ||
"js", | ||
"json" | ||
], | ||
"collectCoverage": true, | ||
"collectCoverageFrom": [ | ||
"src/*.{js,jsx}", | ||
"src/*/*.{js,jsx}", | ||
"!**/node_modules/**", | ||
"!**/vendor/**" | ||
], | ||
"moduleDirectories": [ | ||
"node_modules", | ||
"src" | ||
], | ||
"transform": { | ||
"^.+\\.js?$": "babel-jest" | ||
} | ||
} | ||
} |
Oops, something went wrong.