├── .travis.yml ├── .npmignore ├── .gitignore ├── README.md ├── index.js ├── package.json ├── LICENSE └── test └── main.js /.travis.yml: -------------------------------------------------------------------------------- 1 | language: node_js 2 | node_js: 3 | - "0.10" 4 | -------------------------------------------------------------------------------- /.npmignore: -------------------------------------------------------------------------------- 1 | .DS_Store 2 | *.log 3 | node_modules 4 | build 5 | *.node 6 | components -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | # Logs 2 | logs 3 | *.log 4 | 5 | # Runtime data 6 | pids 7 | *.pid 8 | *.seed 9 | 10 | # Directory for instrumented libs generated by jscoverage/JSCover 11 | lib-cov 12 | 13 | # Coverage directory used by tools like istanbul 14 | coverage 15 | 16 | # Grunt intermediate storage (http://gruntjs.com/creating-plugins#storing-task-files) 17 | .grunt 18 | 19 | # Compiled binary addons (http://nodejs.org/api/addons.html) 20 | build/Release 21 | 22 | # Dependency directory 23 | # Deployed apps should consider commenting this line out: 24 | # see https://npmjs.org/doc/faq.html#Should-I-check-my-node_modules-folder-into-git 25 | node_modules 26 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # gulp-natural-sort 2 | 3 | [![Build Status](https://travis-ci.org/emgeee/gulp-natural-sort.svg?branch=master)](https://travis-ci.org/emgeee/gulp-natural-sort) 4 | 5 | >Sort stream by path name using a natural sort 6 | 7 | ## Installation 8 | 9 | ```shell 10 | npm install --save-dev gulp-natural-sort 11 | ``` 12 | 13 | ## Usage 14 | 15 | To reorder stream in ascending order: 16 | 17 | ```javascript 18 | return gulp.src(path.join(paths.tutorials, folder, 'markdown/*.md')) 19 | .pipe(naturalSort()) 20 | .pipe(gulp.dest(paths.output)); 21 | ``` 22 | 23 | To reorder stream in descending order pass the string `'desc'`: 24 | 25 | ```javascript 26 | .pipe(naturalSort('desc')) 27 | ``` 28 | 29 | ### License 30 | MIT 31 | -------------------------------------------------------------------------------- /index.js: -------------------------------------------------------------------------------- 1 | var through = require('through'); 2 | 3 | function gulpNaturalSort(order) { 4 | 5 | var files = []; 6 | 7 | function onFile(file) { 8 | files.push(file); 9 | } 10 | 11 | function onEnd() { 12 | var ncl = require('natural-compare-lite'); 13 | var _this = this; 14 | 15 | if (ncl !== undefined) { 16 | String.naturalCompare = ncl; 17 | } 18 | 19 | files.sort(function(a, b) { 20 | return String.naturalCompare(a.relative.toLowerCase(), b.relative.toLowerCase()); 21 | }); 22 | 23 | if(order === 'desc') { 24 | files.reverse(); 25 | } 26 | 27 | files.forEach(function(file) { 28 | _this.emit('data', file); 29 | }); 30 | 31 | return this.emit('end'); 32 | } 33 | 34 | return through(onFile, onEnd); 35 | } 36 | 37 | module.exports = gulpNaturalSort; 38 | 39 | -------------------------------------------------------------------------------- /package.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "gulp-natural-sort", 3 | "version": "0.1.1", 4 | "description": "Sort stream by path name using a natural sort", 5 | "main": "index.js", 6 | "keywords": [ 7 | "gulpplugin", 8 | "gulpfriendly", 9 | "gulp", 10 | "sort", 11 | "order", 12 | "natural sort", 13 | "natural" 14 | ], 15 | "scripts": { 16 | "test": "mocha" 17 | }, 18 | "repository": { 19 | "type": "git", 20 | "url": "https://github.com/emgeee/gulp-natural-sort.git" 21 | }, 22 | "author": "Matt Green (http://emgeee.com/)", 23 | "license": "MIT", 24 | "bugs": { 25 | "url": "https://github.com/emgeee/gulp-natural-sort/issues" 26 | }, 27 | "homepage": "https://github.com/emgeee/gulp-natural-sort", 28 | "dependencies": { 29 | "natural-compare-lite": "^1.0.0", 30 | "through": "^2.3.4" 31 | }, 32 | "devDependencies": { 33 | "mocha": "^1.21.3", 34 | "should": "^4.0.4", 35 | "vinyl-fs-mock": "^0.1.2" 36 | } 37 | } 38 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | The MIT License (MIT) 2 | 3 | Copyright (c) 2014 Matt Green 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. 22 | -------------------------------------------------------------------------------- /test/main.js: -------------------------------------------------------------------------------- 1 | var naturalSort = require('../'); 2 | var through = require('through'); 3 | var should = require('should'); 4 | var createFS = require('vinyl-fs-mock'); 5 | require('mocha'); 6 | 7 | describe('gulp-natural-sort', function() { 8 | function generateStream() { 9 | return createFS({ 10 | '2-test.md': 'one', 11 | '1-test.md': 'two', 12 | '10-test.md': 'ten', 13 | '3-test.md': 'three', 14 | }); 15 | 16 | } 17 | 18 | it('should order files in ascending order', function(done) { 19 | var files = []; 20 | 21 | generateStream().src('*.md') 22 | .pipe(naturalSort()) 23 | .pipe(through(function(file) { 24 | files.push(file); 25 | }, function() { 26 | files[0].relative.should.startWith('1-'); 27 | files[1].relative.should.startWith('2-'); 28 | files[2].relative.should.startWith('3-'); 29 | files[3].relative.should.startWith('10-'); 30 | return done(); 31 | })); 32 | }); 33 | 34 | it('should order files in descending order', function(done) { 35 | var files = []; 36 | 37 | generateStream().src('*.md') 38 | .pipe(naturalSort('desc')) 39 | .pipe(through(function(file) { 40 | files.push(file); 41 | }, function() { 42 | files[0].relative.should.startWith('10-'); 43 | files[1].relative.should.startWith('3-'); 44 | files[2].relative.should.startWith('2-'); 45 | files[3].relative.should.startWith('1-'); 46 | return done(); 47 | })); 48 | }); 49 | 50 | 51 | }); 52 | --------------------------------------------------------------------------------