-
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.
Merge pull request #5 from dailyrandomphoto/seq-functions
Implement seq, prefix-seq, date-seq generators. #1
- Loading branch information
Showing
28 changed files
with
321 additions
and
10 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
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,22 @@ | ||
'use strict'; | ||
|
||
const prefixSeq = require('./prefix-seq'); | ||
|
||
module.exports = function(option) { | ||
option = option || {}; | ||
// date prefix sample: | ||
// YYYYMMDD (default) | ||
// YYYY-MM-DD- | ||
// YYMMDD- | ||
// YYYYMM | ||
// YYYY | ||
const pattern = typeof option.prefix === 'string' ? option.prefix : 'YYYYMMDD'; | ||
const date = new Date(); | ||
const prefix = pattern.replace(/YYYY/, date.getFullYear()) | ||
.replace(/YY/, ('' + date.getFullYear()).substring(2)) | ||
.replace(/MM/, ('0' + (date.getMonth() + 1)).substr(-2)) | ||
.replace(/DD/, ('0' + date.getDate()).substr(-2)); | ||
option.prefix = prefix; | ||
option.size = parseInt(option.size, 10) || 2; | ||
return prefixSeq(option); | ||
}; |
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
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 |
---|---|---|
@@ -1,8 +1,26 @@ | ||
'use strict'; | ||
|
||
const chalk = require('chalk'); | ||
const { join, basename } = require('path'); | ||
const { listDirSync } = require('hexo-fs'); | ||
module.exports = function(option) { | ||
option = option || {}; | ||
let start = typeof option.start !== 'undefined' ? option.start : 1; | ||
let prefix = typeof option.prefix !== 'undefined' ? option.prefix : 'prefix-'; | ||
return () => prefix + start++; | ||
const log = option.context ? option.context.log : console; | ||
const start = parseInt(option.start, 10) || 1; | ||
const size = parseInt(option.size, 10) || 1; | ||
const prefix = typeof option.prefix === 'string' ? option.prefix : ''; | ||
const pattern = new RegExp(`^${prefix}\\d+\\.md`); | ||
log.debug('[hexo-unique-post-path] File name pattern: %s', chalk.magenta(pattern)); | ||
const sourceDir = option.context ? option.context.source_dir : join(__dirname, '../../test/source'); | ||
const postDir = join(sourceDir, '_posts'); | ||
const draftDir = join(sourceDir, '_drafts'); | ||
|
||
return function() { | ||
let files = listDirSync(postDir).concat(listDirSync(draftDir)); | ||
files = files.map((file) => basename(file)) | ||
.filter((file) => pattern.test(file)) | ||
.map((file) => parseInt(file.replace(prefix, '').replace('.md', ''), 10)) | ||
.sort(function(a, b) { return b - a; }); | ||
let max = Math.max((files[0] || 0) + 1, start); | ||
return prefix + ('' + max).padStart(size, '0'); | ||
}; | ||
}; |
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 |
---|---|---|
@@ -1,7 +1,10 @@ | ||
'use strict'; | ||
|
||
const prefixSeq = require('./prefix-seq'); | ||
|
||
module.exports = function(option) { | ||
option = option || {}; | ||
let start = typeof option.start !== 'undefined' ? option.start : 1; | ||
return () => '' + start++; | ||
option.prefix = ''; | ||
|
||
return prefixSeq(option); | ||
}; |
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
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
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
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,100 @@ | ||
'use strict'; | ||
|
||
const generators = require('../../lib/id-generators'); | ||
|
||
describe('date-seq', () => { | ||
it('should return a string with a date prefix and have length of 10: default size', () => { | ||
const generator = generators.get('date-seq'); | ||
generator.should.be.a('function'); | ||
const gen = generator({}); | ||
gen.should.be.a('function'); | ||
const id = gen(); | ||
console.log(' id: ' + id); | ||
id.should.be.a('string').match(/^\d{10}/); | ||
parseInt(id, 10).should.be.a('number').not.be.NaN; | ||
}); | ||
|
||
it('should return a string with a date prefix and have length of 10', () => { | ||
const generator = generators.get('date-seq'); | ||
generator.should.be.a('function'); | ||
const gen = generator({size: 2}); | ||
gen.should.be.a('function'); | ||
const id = gen(); | ||
console.log(' id: ' + id); | ||
id.should.be.a('string').match(/^\d{10}$/); | ||
}); | ||
|
||
it('should return a string with a date prefix and have length of 12', () => { | ||
const generator = generators.get('date-seq'); | ||
generator.should.be.a('function'); | ||
const gen = generator({size: 4}); | ||
gen.should.be.a('function'); | ||
const id = gen(); | ||
console.log(' id: ' + id); | ||
id.should.be.a('string').match(/^\d{12}$/); | ||
}); | ||
|
||
it('should return a string with seq is 99', () => { | ||
const generator = generators.get('date-seq'); | ||
generator.should.be.a('function'); | ||
const gen = generator({start: 99}); | ||
gen.should.be.a('function'); | ||
const id = gen(); | ||
console.log(' id: ' + id); | ||
id.should.be.a('string').match(/^\d{8}99$/); | ||
}); | ||
|
||
it('should return a string with a custom date prefix: YYYY-MM-DD-', () => { | ||
const generator = generators.get('date-seq'); | ||
generator.should.be.a('function'); | ||
const gen = generator({prefix: 'YYYY-MM-DD-', size: 4}); | ||
gen.should.be.a('function'); | ||
const id = gen(); | ||
console.log(' id: ' + id); | ||
id.should.be.a('string').match(/^\d{4}-\d{2}-\d{2}-\d{4}$/); | ||
}); | ||
|
||
it('should return a string with a custom date prefix: YY-MM-DD-', () => { | ||
const generator = generators.get('date-seq'); | ||
generator.should.be.a('function'); | ||
const gen = generator({prefix: 'YY-MM-DD-', size: 4}); | ||
gen.should.be.a('function'); | ||
const id = gen(); | ||
console.log(' id: ' + id); | ||
id.should.be.a('string').match(/^\d{2}-\d{2}-\d{2}-\d{4}$/); | ||
}); | ||
|
||
it('should return a string with a custom date prefix: YYYY_', () => { | ||
const generator = generators.get('date-seq'); | ||
generator.should.be.a('function'); | ||
const gen = generator({prefix: 'YYYY_', size: 4}); | ||
gen.should.be.a('function'); | ||
const id = gen(); | ||
console.log(' id: ' + id); | ||
id.should.be.a('string').match(/^\d{4}_\d{4}$/); | ||
}); | ||
|
||
it('should return a string with a custom date prefix: TEST_YYYY_', () => { | ||
const generator = generators.get('date-seq'); | ||
generator.should.be.a('function'); | ||
const gen = generator({prefix: 'TEST_YYYY_', size: 4}); | ||
gen.should.be.a('function'); | ||
const id = gen(); | ||
console.log(' id: ' + id); | ||
id.should.be.a('string').match(/^TEST_\d{4}_\d{4}$/); | ||
}); | ||
|
||
it('should return a string with seq start from 1', () => { | ||
const generator = generators.get('date-seq'); | ||
generator.should.be.a('function'); | ||
const gen = generator({prefix: 'items-', size: 'a NaN', start: 'a NaN'}); | ||
gen.should.be.a('function'); | ||
const id = gen(); | ||
console.log(' id: ' + id); | ||
id.should.be.a('string').match(/^items-\d+/); | ||
|
||
const gen2 = generator({prefix: 'items-', size: 1, start: 1}); | ||
const id2 = gen2(); | ||
id.should.eql(id2); | ||
}); | ||
}); |
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
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,72 @@ | ||
'use strict'; | ||
|
||
const generators = require('../../lib/id-generators'); | ||
|
||
describe('prefix-seq', () => { | ||
it('should return a string with an empty prefix', () => { | ||
const generator = generators.get('prefix-seq'); | ||
generator.should.be.a('function'); | ||
const gen = generator({}); | ||
gen.should.be.a('function'); | ||
const id = gen(); | ||
console.log(' id: ' + id); | ||
id.should.be.a('string').match(/^\d+/); | ||
parseInt(id, 10).should.be.a('number').not.be.NaN; | ||
}); | ||
|
||
it('should return a string with a prefix', () => { | ||
const generator = generators.get('prefix-seq'); | ||
generator.should.be.a('function'); | ||
const gen = generator({prefix: 'items-'}); | ||
gen.should.be.a('function'); | ||
const id = gen(); | ||
console.log(' id: ' + id); | ||
id.should.be.a('string').match(/^items-\d+/); | ||
}); | ||
|
||
it('should return a string with seq is 99', () => { | ||
const generator = generators.get('prefix-seq'); | ||
generator.should.be.a('function'); | ||
const gen = generator({prefix: 'items-', start: 99}); | ||
gen.should.be.a('function'); | ||
const id = gen(); | ||
console.log(' id: ' + id); | ||
id.should.be.a('string').match(/^items-\d+/); | ||
id.should.eql('items-99'); | ||
}); | ||
|
||
it('should return a string with length is 10', () => { | ||
const generator = generators.get('prefix-seq'); | ||
generator.should.be.a('function'); | ||
const gen = generator({prefix: 'items-', size: 4}); | ||
gen.should.be.a('function'); | ||
const id = gen(); | ||
console.log(' id: ' + id); | ||
id.should.be.a('string').match(/^items-\d+/).have.lengthOf(10); | ||
}); | ||
|
||
it('should return a string with length is 10', () => { | ||
const generator = generators.get('prefix-seq'); | ||
generator.should.be.a('function'); | ||
const gen = generator({prefix: 'items-', size: 4, start: 99}); | ||
gen.should.be.a('function'); | ||
const id = gen(); | ||
console.log(' id: ' + id); | ||
id.should.be.a('string').match(/^items-\d+/).have.lengthOf(10); | ||
id.should.eql('items-0099'); | ||
}); | ||
|
||
it('should return a string with seq start from 1', () => { | ||
const generator = generators.get('prefix-seq'); | ||
generator.should.be.a('function'); | ||
const gen = generator({prefix: 'items-', size: 'a NaN', start: 'a NaN'}); | ||
gen.should.be.a('function'); | ||
const id = gen(); | ||
console.log(' id: ' + id); | ||
id.should.be.a('string').match(/^items-\d+/); | ||
|
||
const gen2 = generator({prefix: 'items-', size: 1, start: 1}); | ||
const id2 = gen2(); | ||
id.should.eql(id2); | ||
}); | ||
}); |
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,66 @@ | ||
'use strict'; | ||
|
||
const generators = require('../../lib/id-generators'); | ||
|
||
describe('seq', () => { | ||
it('should return a string', () => { | ||
const generator = generators.get('seq'); | ||
generator.should.be.a('function'); | ||
const gen = generator({}); | ||
gen.should.be.a('function'); | ||
const id = gen(); | ||
console.log(' id: ' + id); | ||
id.should.be.a('string'); | ||
parseInt(id, 10).should.be.a('number').not.be.NaN; | ||
}); | ||
|
||
it('should return a string with length is 2', () => { | ||
const generator = generators.get('seq'); | ||
generator.should.be.a('function'); | ||
const gen = generator({start: 99}); | ||
gen.should.be.a('function'); | ||
const id = gen(); | ||
console.log(' id: ' + id); | ||
id.should.be.a('string').have.lengthOf(2); | ||
id.should.eql('99'); | ||
parseInt(id, 10).should.be.a('number').not.be.NaN; | ||
}); | ||
|
||
it('should return a string with length is 4', () => { | ||
const generator = generators.get('seq'); | ||
generator.should.be.a('function'); | ||
const gen = generator({size: 4}); | ||
gen.should.be.a('function'); | ||
const id = gen(); | ||
console.log(' id: ' + id); | ||
id.should.be.a('string').have.lengthOf(4); | ||
parseInt(id, 10).should.be.a('number').not.be.NaN; | ||
}); | ||
|
||
it('should return a string with length is 4', () => { | ||
const generator = generators.get('seq'); | ||
generator.should.be.a('function'); | ||
const gen = generator({size: 4, start: 99}); | ||
gen.should.be.a('function'); | ||
const id = gen(); | ||
console.log(' id: ' + id); | ||
id.should.be.a('string').have.lengthOf(4); | ||
id.should.eql('0099'); | ||
parseInt(id, 10).should.be.a('number').not.be.NaN; | ||
}); | ||
|
||
it('should return a string with seq start from 1', () => { | ||
const generator = generators.get('seq'); | ||
generator.should.be.a('function'); | ||
const gen = generator({size: 'a NaN', start: 'a NaN'}); | ||
gen.should.be.a('function'); | ||
const id = gen(); | ||
console.log(' id: ' + id); | ||
id.should.be.a('string').match(/^\d+/); | ||
parseInt(id, 10).should.be.a('number').not.be.NaN; | ||
|
||
const gen2 = generator({size: 1, start: 1}); | ||
const id2 = gen2(); | ||
id.should.eql(id2); | ||
}); | ||
}); |
Empty file.
Empty file.
Empty file.
Empty file.
Empty file.
Empty file.
Empty file.
Empty file.
Empty file.
Empty file.
Empty file.
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 @@ | ||
../symbolictest |
Empty file.
Empty file.
Empty file.
Empty file.