sequence
Define custom 'number' sequences in a collection, using the nodejs number-sequence library, with optional prefix and suffix.
Introduction
This meteor library lets you define and use a your own sequence, which might be useful for order numbers, customer numbers etc.
Think 'autonumber' or 'auto_increment', but more flexible (albeit less efficient, of course).
You can define a sequence like 00129QXZ, LONDON-ORDER-ABC123*-DRAFT, or PREFIX-00123-SUFFIX, or a simple 'numeric' sequence (although this library is overkill if that's all you need).
Note: Be certain to make the sequence length larger than you are likely to need; Otherwise it will 'roll over' and you are likely to be left with duplicates.
Installation
meteor npm install number-sequence --save meteor add juto:sequence
Usage
See examples below. You need to provide:
- a number system, which is a set of 'digit' characters (as a string) in increasing order of significance, from left to right.
- a numeric length, which is the number of 'digits' you will be maintaining in the sequence.
- a padding character, to left-pad shorter sequences to the value you specified in the length parameter.
- (optional) a string prefix to prepend to the sequence before returning.
- (optional) a string suffix to append to the sequence before returning.
Example 1 : Two digits, decimal (base 10)
1import sequence from 'meteor/juto:sequence'; 2 3// define a new 2-digit sequence and give it a name 4sequence.newSequence( 5 "twoDigitBaseTen", // sequence name (must be unique). 6 { 7 numsys: "0123456789", // the 'digits' to be used in the sequence 8 padChar: "0", // the character to left-pad with if the value is not long enough. 9 length: 2 // the number of characters in the sequence; the sequence will 'wrap around' if it runs out of values. 10 }, 11 "00" // the initial value of the sequence 12).then(function (id) { 13 console.log(`created a new sequence with id ${id}`); 14}); 15 16const numValues = sequence.calculateNumberOfPossibleValues("twoDigitBaseTen"); // 100 possible values, since we have 2 digits. 17 18// get the next value in the sequence 19sequence.getAndUpdateNextNumberInSequence("twoDigitBaseTen") 20.then(function (nextInSequence) { 21 console.log(nextInSequence); // 01 22}); 23 24// get the next value in the sequence 25sequence.getAndUpdateNextNumberInSequence("twoDigitBaseTen") 26.then(function (nextInSequence) { 27 console.log(nextInSequence); // 02 28}); 29
Example 2 : Two digits, hexadecimal (base 16)
Note that this time we're setting the initial value to 'EF' so that the next value should be 'F0'
1// define a new 2-digit sequence and give it a name 2sequence.newSequence( 3 "hex", // sequence name (must be unique). 4 { 5 numsys: "0123456789ABCDEF", // the 'digits' to be used in the sequence 6 padChar: "0", // the character to left-pad with if the value is not long enough. 7 length: 2 // the number of characters in the sequence; the sequence will 'wrap around' if it runs out of values. 8 }, 9 "EF" // the initial value of the sequence 10).then(function (id) { 11 console.log(`created a new sequence with id ${id}`); 12}); 13 14const numValues = sequence.calculateNumberOfPossibleValues("hex"); // 256 possible values, since we have 2 digits. 15 16// get the next value in the sequence 17sequence.getAndUpdateNextNumberInSequence("hex") 18.then(function (nextInSequence) { 19 console.log(nextInSequence); // F0 20}); 21 22// get the next value in the sequence 23sequence.getAndUpdateNextNumberInSequence("hex") 24.then(function (nextInSequence) { 25 console.log(nextInSequence); // F1 26});
Example 3 : Two digits, hexadecimal, with prefix and suffix (base 16)
Note that this time we're setting the initial value to 'EF' so that the next value should be 'F0'
1// define a new 2-digit sequence and give it a name 2sequence.newSequence( 3 "hex", // sequence name (must be unique). 4 { 5 numsys: "0123456789ABCDEF", // the 'digits' to be used in the sequence 6 padChar: "0", // the character to left-pad with if the value is not long enough. 7 length: 2, // the number of characters in the sequence; the sequence will 'wrap around' if it runs out of values. 8 prefix: "PRE-", 9 suffix: "-SUFF" 10 }, 11 "EF" // the initial value of the sequence 12).then(function (id) { 13 console.log(`created a new sequence with id ${id}`); 14}); 15 16const numValues = sequence.calculateNumberOfPossibleValues("hexWithPrefixSuffix"); // 256 possible values, since we have 2 digits. 17 18// get the next value in the sequence 19sequence.getAndUpdateNextNumberInSequence("hexWithPrefixSuffix") 20.then(function (nextInSequence) { 21 console.log(nextInSequence); // PRE-F0-SUFF 22}); 23 24// get the next value in the sequence 25sequence.getAndUpdateNextNumberInSequence("hexWithPrefixSuffix") 26.then(function (nextInSequence) { 27 console.log(nextInSequence); // PRE-F1-SUFF 28});