Skip to content

Instantly share code, notes, and snippets.

@dluciv
Forked from 140bytes/LICENSE.txt
Last active December 29, 2015 07:49
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save dluciv/7638511 to your computer and use it in GitHub Desktop.
Save dluciv/7638511 to your computer and use it in GitHub Desktop.

140byt.es

A tweet-sized, fork-to-play, community-curated collection of JavaScript.

How to play

  1. Click the Fork button above to fork this gist.
  2. Modify all the files to according to the rules below.
  3. Save your entry and tweet it up!

Keep in mind that thanks to the awesome sensibilities of the GitHub team, gists are just repos. So feel free to clone yours and work locally for a more comfortable environment, and to allow commit messages.

Rules

All entries must exist in an index.js file, whose contents are

  1. an assignable, valid Javascript expression that
  2. contains no more than 140 bytes, and
  3. does not leak to the global scope.

All entries must also be licensed under the WTFPL or equally permissive license.

For more information

See the 140byt.es site for a showcase of entries (built itself using 140-byte entries!), and follow @140bytes on Twitter.

To learn about byte-saving hacks for your own code, or to contribute what you've learned, head to the wiki.

140byt.es is brought to you by Jed Schmidt, with help from Alex Kloss. It was inspired by work from Thomas Fuchs and Dustin Diaz.

// This function gets 7 first digits of ISSN (8th is control digit/character)
// + 2 variant digits (9 total) and calculates:
// - 8th check digit/character barcode http://www.loc.gov/issn/check.html
// - 13th check digit for ISSN EAN-13 http://www.ehow.com/how_5135127_calculate-ean-check-digit.html
function(issn9) {
var c = 9, d, eanc = 0,
eanm = 1, issnc = 0, issnm = 8;
for (; c--; ) { // reverse loop to shorten code
d = -issn9[c]; // to convert it from character (and get negative)
// EAN-13 control sum is calculated as
// 1*d0 + 3*d1 + 1*d2 + ... + 1*d12
// and then getting addition to get closesd upper 10-multiple
// here we start with d3 in fact, bypassing starting '977',
// we will take it into account later.
eanc += (2 - (eanm = -eanm)) * d;
// ISSN control sum is calculated as
// 8*d0 + 7*d1 + .. 2*d6
// and then getting addition to get closesd upper 11-multiple,
// if we got 10 (2 digits), we should print 'X'
issnc += d * (8 - c) * (c < 7); // sum only for 7 first digits
}
// here we have negative values of issn and ean (without starting 977 digits)
// counters. Max values for counters are
// 9*(8+7+6+5+4+3+2) = 315 for issn, going up to closest 11 multiple we get 319
// so we add 319 to negated counter and calculate %11 to get the addition.
issnc = (319 + issnc) % 11;
// 9*3 + 7*1 + 7*3 + #9 more gidits mul 1 or 3# 5 x 1 * 9 + 4 x 3 * 9 = 208
// for ean, going up to closest 10 multiple we get 210.
// And then dont forget to subtract 1*9 + 3*7 + 1*7 = 37 for first 3 ean digits,
// Getting 210 - 37 = 173.
// so we add 173 to negated counter and calculate %10 to get the addition.
return [issnc ^ 10 ? issnc : 'X', (173 + eanc) % 10]
// issnc ^ 10 will be non zero (e.g. true) when issnc != 10
}
function(f){for(var b=9,c,d=0,e=1,a=0;b--;)c=-f[b],d+=(2-(e=-e))*c,a+=c*(8-b)*(7>b);a=(319+a)%11;return[a^10?a:"X",(173+d)%10]}
DO WHAT THE FUCK YOU WANT TO PUBLIC LICENSE
Version 2, December 2004
Copyright (C) 2011 YOUR_NAME_HERE <YOUR_URL_HERE>
Everyone is permitted to copy and distribute verbatim or modified
copies of this license document, and changing it is allowed as long
as the name is changed.
DO WHAT THE FUCK YOU WANT TO PUBLIC LICENSE
TERMS AND CONDITIONS FOR COPYING, DISTRIBUTION AND MODIFICATION
0. You just DO WHAT THE FUCK YOU WANT TO.
{
"name": "issnEanCheckDigitsCalculator",
"description": "This function calculates check digit/character for ISSN and its EAN-13 barcode, including 2-digit variant",
"keywords": [
"ISSN",
"EAN-13",
"barcode",
"check"
]
}
<!DOCTYPE html>
<title>ISSN + EAN-13 chack calculator</title>
<body>
<p>Log:</p>
<p id="log"></p>
</body>
<script>
// write a small example that shows off the API for your example
// and tests it in one fell swoop.
var issn2ie = function(f){for(var b=9,c,d=0,e=1,a=0;b--;)c=-f[b],d+=(2-(e=-e))*c,a+=c*(8-b)*(7>b);a=(319+a)%11;return[a^10?a:"X",(173+d)%10]};
function addLog(s) {
document.getElementById( "log" ).innerHTML += ("<br/>" + s);
}
function dotest(s, i, e) {
addLog("For: " + s);
var res = issn2ie(s);
addLog('' + i + ', ' + e + ' expected,');
addLog('' + res[0] + ', ' + res[1] + ' calculated.');
}
dotest('023356711', 0, 3);
dotest('123456789', 9, 8);
dotest('923723994', 2, 1);
dotest('999999999', 4, 2);
dotest('000000000', 0, 3);
dotest('100010000', 'X', 7);
</script>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment