123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167 |
- var VersionCheck = require('./version-check')
- var Regex = require('./regex')
- exports.NUMERIC = {
- id: 'Numeric',
- bit: 1 << 0,
- ccBits: [10, 12, 14]
- }
- exports.ALPHANUMERIC = {
- id: 'Alphanumeric',
- bit: 1 << 1,
- ccBits: [9, 11, 13]
- }
- exports.BYTE = {
- id: 'Byte',
- bit: 1 << 2,
- ccBits: [8, 16, 16]
- }
- exports.KANJI = {
- id: 'Kanji',
- bit: 1 << 3,
- ccBits: [8, 10, 12]
- }
- exports.MIXED = {
- bit: -1
- }
- exports.getCharCountIndicator = function getCharCountIndicator (mode, version) {
- if (!mode.ccBits) throw new Error('Invalid mode: ' + mode)
- if (!VersionCheck.isValid(version)) {
- throw new Error('Invalid version: ' + version)
- }
- if (version >= 1 && version < 10) return mode.ccBits[0]
- else if (version < 27) return mode.ccBits[1]
- return mode.ccBits[2]
- }
- exports.getBestModeForData = function getBestModeForData (dataStr) {
- if (Regex.testNumeric(dataStr)) return exports.NUMERIC
- else if (Regex.testAlphanumeric(dataStr)) return exports.ALPHANUMERIC
- else if (Regex.testKanji(dataStr)) return exports.KANJI
- else return exports.BYTE
- }
- exports.toString = function toString (mode) {
- if (mode && mode.id) return mode.id
- throw new Error('Invalid mode')
- }
- exports.isValid = function isValid (mode) {
- return mode && mode.bit && mode.ccBits
- }
- function fromString (string) {
- if (typeof string !== 'string') {
- throw new Error('Param is not a string')
- }
- var lcStr = string.toLowerCase()
- switch (lcStr) {
- case 'numeric':
- return exports.NUMERIC
- case 'alphanumeric':
- return exports.ALPHANUMERIC
- case 'kanji':
- return exports.KANJI
- case 'byte':
- return exports.BYTE
- default:
- throw new Error('Unknown mode: ' + string)
- }
- }
- exports.from = function from (value, defaultValue) {
- if (exports.isValid(value)) {
- return value
- }
- try {
- return fromString(value)
- } catch (e) {
- return defaultValue
- }
- }
|