Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 25 additions & 0 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
name: CI

on:
push:
branches: [ "work", "master", "main" ]
pull_request:

jobs:
test:
name: Node.js ${{ matrix.node-version }}
runs-on: ubuntu-latest
strategy:
fail-fast: false
matrix:
node-version: ["4.x", "6.x", "8.x", "10.x", "12.x", "14.x", "16.x", "18.x", "20.x"]
steps:
- uses: actions/checkout@v4
- name: Use Node.js ${{ matrix.node-version }}
uses: actions/setup-node@v4
with:
node-version: ${{ matrix.node-version }}
- name: Install dependencies
run: npm install --ignore-scripts
- name: Run tests
run: npm test -- --reporter dot
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
# node-scanf [![NPM Version](https://badge.fury.io/js/scanf.svg)](http://badge.fury.io/js/scanf) [![Build Status](https://travis-ci.org/Lellansin/node-scanf.png?branch=master)](https://travis-ci.org/Lellansin/node-scanf) [![Coveralls Status](https://img.shields.io/coveralls/Lellansin/node-scanf/master.svg)](https://coveralls.io/github/Lellansin/node-scanf)
# node-scanf [![NPM Version](https://badge.fury.io/js/scanf.svg)](http://badge.fury.io/js/scanf) [![CI](https://github.com/Lellansin/node-scanf/actions/workflows/ci.yml/badge.svg)](https://github.com/Lellansin/node-scanf/actions/workflows/ci.yml)

Do you want simplely shell script input which have formats and sync return?

Expand Down
81 changes: 44 additions & 37 deletions lib/gets.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,43 +8,50 @@ var BUFSIZE = 256;
var buf = Buffer.alloc ? Buffer.alloc(BUFSIZE) : new Buffer(BUFSIZE);
var bytesRead;

module.exports = function() {
var fd =
'win32' === process.platform
? process.stdin.fd
: fs.openSync('/dev/stdin', 'rs');
bytesRead = 0;

try {
bytesRead = fs.readSync(fd, buf, 0, BUFSIZE);
} catch (e) {
if (e.code === 'EAGAIN') {
// 'resource temporarily unavailable'
// Happens on OS X 10.8.3 (not Windows 7!), if there's no
// stdin input - typically when invoking a script without any
// input (for interactive stdin input).
// If you were to just continue, you'd create a tight loop.
console.error('ERROR: interactive stdin input not supported.');
process.exit(1);
} else if (e.code === 'EOF') {
// Happens on Windows 7, but not OS X 10.8.3:
// simply signals the end of *piped* stdin input.
var createGets = function(customFs) {
var localFs = customFs || fs;

return function() {
var fd =
'win32' === process.platform
? process.stdin.fd
: localFs.openSync('/dev/stdin', 'rs');
bytesRead = 0;

try {
bytesRead = localFs.readSync(fd, buf, 0, BUFSIZE);
} catch (e) {
if (e.code === 'EAGAIN') {
// 'resource temporarily unavailable'
// Happens on OS X 10.8.3 (not Windows 7!), if there's no
// stdin input - typically when invoking a script without any
// input (for interactive stdin input).
// If you were to just continue, you'd create a tight loop.
console.error('ERROR: interactive stdin input not supported.');
process.exit(1);
} else if (e.code === 'EOF') {
// Happens on Windows 7, but not OS X 10.8.3:
// simply signals the end of *piped* stdin input.
return '';
}
throw e; // unexpected exception
}

if (bytesRead === 0) {
// No more stdin input available.
// OS X 10.8.3: regardless of input method, this is how the end
// of input is signaled.
// Windows 7: this is how the end of input is signaled for
// *interactive* stdin input.
return '';
}
throw e; // unexpected exception
}

if (bytesRead === 0) {
// No more stdin input available.
// OS X 10.8.3: regardless of input method, this is how the end
// of input is signaled.
// Windows 7: this is how the end of input is signaled for
// *interactive* stdin input.
return '';
}
// Process the chunk read.

var content = buf.toString(undefined, 0, bytesRead - 1);

return content;
// Process the chunk read.

var content = buf.toString(undefined, 0, bytesRead - 1);

return content;
};
};

module.exports = createGets();
module.exports.withFs = createGets;
12 changes: 6 additions & 6 deletions lib/scanf.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,6 @@ var gets = require('./gets');
var input = '';
var stdin_flag = true;

exports.throw = true;

var scanf = (module.exports = function(format) {
var re = new RegExp('[^%]*%[0-9]*[A-Za-z][^%]*', 'g');
var selector = format.match(re);
Expand Down Expand Up @@ -46,6 +44,8 @@ var scanf = (module.exports = function(format) {
return result;
});

scanf.throw = true;

module.exports.sscanf = function(str, format) {
if (typeof str !== 'string' || !str.length) {
return null;
Expand Down Expand Up @@ -121,7 +121,7 @@ var getInteger = function(pre, next) {
return utils.hex2int(text);
}
catch(e) {
if(exports.throw) return NaN
if(scanf.throw) return NaN

return null
}
Expand All @@ -132,7 +132,7 @@ var getInteger = function(pre, next) {
return utils.octal2int(text);
}
catch(e) {
if(exports.throw) return NaN
if(scanf.throw) return NaN

return null
}
Expand Down Expand Up @@ -181,7 +181,7 @@ var getHex = function(pre, next) {
return utils.hex2int(text);
}
catch(e) {
if(exports.throw) return NaN
if(scanf.throw) return NaN

return null
}
Expand All @@ -193,7 +193,7 @@ var getOctal = function(pre, next) {
return utils.octal2int(text);
}
catch(e) {
if(exports.throw) return NaN
if(scanf.throw) return NaN

return null
}
Expand Down
Loading