Skip to content

Commit

Permalink
feat: impliment isAscii
Browse files Browse the repository at this point in the history
  • Loading branch information
ariporad committed Jan 6, 2016
1 parent bec7a75 commit 6b86fda
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 0 deletions.
13 changes: 13 additions & 0 deletions index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
/* (c) 2016 Ari Porad (@ariporad) <http://ariporad.com>. License: ariporad.mit-license.org */

// Partially from http://stackoverflow.com/a/94049/1928484, and from another SO answer, which told me that the highest
// char code that's ascii is 127, but I can't find the link for. Sorry.

var MAX_ASCII_CHAR_CODE = 127;

module.exports = function isAscii(str) {
for (var i = 0, strLen = str.length; i < strLen; ++i) {
if (str.charCodeAt(i) > MAX_ASCII_CHAR_CODE) return false;
}
return true;
};
11 changes: 11 additions & 0 deletions test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
/* (c) 2016 Ari Porad (@ariporad) <http://ariporad.com>. License: ariporad.mit-license.org */
import test from 'ava';
import isAscii from './index';

test('ascii strings', t => {
t.is(isAscii('foo'), true);
});

test('non-ascii strings', t => {
t.is(isAscii('‽‽‽'), false);
});

0 comments on commit 6b86fda

Please sign in to comment.