Skip to content

Instantly share code, notes, and snippets.

@rcknight
Last active November 21, 2017 20:21
Show Gist options
  • Save rcknight/56fb750d1325683fcf00d8f9a12031f2 to your computer and use it in GitHub Desktop.
Save rcknight/56fb750d1325683fcf00d8f9a12031f2 to your computer and use it in GitHub Desktop.
requirebin sketch
// Welcome! require() some modules from npm (like you were using browserify)
// and then hit Run Code to run your code on the right side.
// Modules get downloaded from browserify-cdn and bundled in your browser.
var gs1js = require("gs1js")
var code = '0105000471007015' // 01 GTIN
+ '2110001136169476' + String.fromCharCode(29) // 21 Serial
//Variable length fields (like this serial number)
//Need to end in a group separator char
//When I copy/pasted the code it got stripped
//So adding it back here - hopefully these chars will survive our
//barcode scan -> http post -> websocket -> chrome journey
+ '17190331' // 17 Expiry
+ '10B3009H34'; // 10 Lot Number
// Call the constructor from the global scope
var myReader = new gs1js.GS1Reader(code);
// Read the found AIs
var AIs = myReader.getApplicationIdentifiers();
console.log(AIs);
for(i = 0; i < AIs.length; i++) {
var AI = AIs[i];
var el = document.createElement('p');
el.innerHTML = 'AppId: ' + AI.identifier + ', Value: ' + AI.value;
document.body.appendChild(el);
}
setTimeout(function(){require=function e(t,n,r){function s(o,u){if(!n[o]){if(!t[o]){var a=typeof require=="function"&&require;if(!u&&a)return a(o,!0);if(i)return i(o,!0);var f=new Error("Cannot find module '"+o+"'");throw f.code="MODULE_NOT_FOUND",f}var l=n[o]={exports:{}};t[o][0].call(l.exports,function(e){var n=t[o][1][e];return s(n?n:e)},l,l.exports,e,t,n,r)}return n[o].exports}var i=typeof require=="function"&&require;for(var o=0;o<r.length;o++)s(r[o]);return s}({1:[function(require,module,exports){"use strict";var ApplicationIdentifier=function(){function ApplicationIdentifier(identifier,value){this.identifier=identifier;this.value=value;this.length=value.length}return ApplicationIdentifier}();Object.defineProperty(exports,"__esModule",{value:true});exports.default=ApplicationIdentifier},{}],2:[function(require,module,exports){"use strict";var GS1Assets=function(){function GS1Assets(){}GS1Assets.FIXED_LENGTH_IDENTIFIERS=[{ai:"00",length:20},{ai:"01",length:16},{ai:"02",length:16},{ai:"03",length:16},{ai:"04",length:18},{ai:"11",length:8},{ai:"12",length:8},{ai:"13",length:8},{ai:"14",length:8},{ai:"15",length:8},{ai:"16",length:8},{ai:"17",length:8},{ai:"18",length:8},{ai:"19",length:8},{ai:"20",length:4},{ai:"31",length:10},{ai:"32",length:10},{ai:"33",length:10},{ai:"34",length:10},{ai:"35",length:10},{ai:"36",length:10},{ai:"41",length:16}];GS1Assets.getFixedLengthIdentifier=function(ai){for(var i=0,l=this.FIXED_LENGTH_IDENTIFIERS.length;i<l;i++){if(this.FIXED_LENGTH_IDENTIFIERS[i].ai===ai){return this.FIXED_LENGTH_IDENTIFIERS[i]}}return null};return GS1Assets}();Object.defineProperty(exports,"__esModule",{value:true});exports.default=GS1Assets},{}],3:[function(require,module,exports){"use strict";var helpers=require("./Helpers");var GS1Assets_1=require("./GS1Assets");var ApplicationIdentifier_1=require("../ApplicationIdentifier");function splitBinAtGS(bytes,gs){var parts=[];var start=0;for(var i=0;i<=gs.length;i++){if(i<gs.length){parts.push(bytes.slice(start,gs[i]));start=gs[i]+1}else{parts.push(bytes.slice(start))}}return parts}exports.splitBinAtGS=splitBinAtGS;function cleanStart(bytes){if(bytes&&bytes.length>0&&bytes[0]==29){bytes.shift()}return bytes}exports.cleanStart=cleanStart;function extractIds(code){if(code.length<=1){return[]}var codeWorking=code;var ids=[];var gap=1;var startPos=0;while(startPos<codeWorking.length){if(gap>300){break}var guessAI=codeWorking.substr(startPos,gap);var binArray=helpers.getASCIIArray(guessAI);var isGS=binArray[binArray.length-1]==29;if(!isGS){var fixedLengthAI=GS1Assets_1.default.getFixedLengthIdentifier(guessAI);if(!fixedLengthAI){if(startPos+gap>=codeWorking.length){var dynamicAI=codeWorking.substr(startPos,gap);var id=dynamicAI.substr(0,2);var value=dynamicAI.substr(2);ids.push(new ApplicationIdentifier_1.default(id,value));break}gap++}else{var lenId=fixedLengthAI.ai.length;var idValue=codeWorking.substr(startPos+lenId,fixedLengthAI.length-lenId);ids.push(new ApplicationIdentifier_1.default(fixedLengthAI.ai,idValue));startPos+=fixedLengthAI.length;gap=1}}else{var dynamicAI=codeWorking.substr(startPos,gap);var id=dynamicAI.substr(0,2);var value=dynamicAI.substr(2);value=value.substr(0,value.length-1);ids.push(new ApplicationIdentifier_1.default(id,value));startPos+=gap;gap=1}}return ids}exports.extractIds=extractIds},{"../ApplicationIdentifier":1,"./GS1Assets":2,"./Helpers":4}],4:[function(require,module,exports){"use strict";function getASCIIArray(str){var asciiArray=[];for(var i=0;i<str.length;i++){asciiArray.push(str.charCodeAt(i))}return asciiArray}exports.getASCIIArray=getASCIIArray;function bin2String(array){return String.fromCharCode.apply(String,array)}exports.bin2String=bin2String},{}],gs1js:[function(require,module,exports){"use strict";var helpers=require("./Helpers/Helpers");var gs1helpers=require("./Helpers/GS1Helpers");var GS1Reader=function(){function GS1Reader(code,bytes){if(bytes===void 0){bytes=[]}this.code=code;this.bytes=bytes;this.checkBytes();this.hasidentifiers=false;this.lookup={};this.extractIdentifiers()}GS1Reader.prototype.checkBytes=function(){if(!this.bytes||this.bytes.length===0){this.bytes=helpers.getASCIIArray(this.code)}this.bytes=gs1helpers.cleanStart(this.bytes);this.code=helpers.bin2String(this.bytes)};GS1Reader.prototype.extractIdentifiers=function(){this.identifiers=gs1helpers.extractIds(this.code);this.hasidentifiers=this.identifiers.length>0;for(var i=0,len=this.identifiers.length;i<len;i++){this.lookup[this.identifiers[i].identifier]=this.identifiers[i]}};GS1Reader.prototype.getApplicationIdentifiers=function(){return this.identifiers};GS1Reader.prototype.getApplicationIdentifierById=function(id){return this.lookup[id]};return GS1Reader}();exports.GS1Reader=GS1Reader},{"./Helpers/GS1Helpers":3,"./Helpers/Helpers":4}]},{},[]);var gs1js=require("gs1js");var code="0105000471007015"+"2110001136169476"+String.fromCharCode(29)+"17190331"+"10B3009H34";var myReader=new gs1js.GS1Reader(code);var AIs=myReader.getApplicationIdentifiers();console.log(AIs);for(i=0;i<AIs.length;i++){var AI=AIs[i];var el=document.createElement("p");el.innerHTML="AppId: "+AI.identifier+", Value: "+AI.value;document.body.appendChild(el)}},0);
{
"name": "requirebin-sketch",
"version": "1.0.0",
"dependencies": {
"gs1js": "0.0.2"
}
}
<!-- contents of this file will be placed inside the <body> -->
<!-- contents of this file will be placed inside the <head> -->
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment