1.2.0 • Published 1 year ago

scanf v1.2.0

Weekly downloads
1,029
License
MIT
Repository
github
Last release
1 year ago

node-scanf NPM Version Build Status Coveralls Status

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

Do you want a sscanf like function to parse format strings?

scanf is a C like scanf/sscanf module for node.js which can help you with that.

Documentation

Installation

npm install scanf

Format support now

  • %d - integer
  • %f - float
  • %s - string
  • %S - string of line
  • %x - hex
  • %a - hex float
  • %o - octal

Example

Quickly start

var scanf = require('scanf');

console.log('Pleas input your name');
var name = scanf('%s');

console.log('Pleas input your age');
var age = scanf('%d');

console.log('your name [%s] type: [%s]', name, typeof name);
console.log('your age [%s] type: [%s]', age, typeof age);

REPL

Pleas input your name
> Barack\ Obama
Pleas input your age
> 24
your name [Barack Obama] type: [string]
your age [24] type: [number]

C like format

var scanf = require('scanf');

console.log('when are you born? \(Year-month-day\)');
var date = scanf('%d-%d-%d');

console.log('your birthday [%s]', date);

REPL

when are you born? (Year-month-day)
> 1990-01-01
your birthday [1990,1,1]

Return

Directly return

var scanf = require('scanf');

var number = scanf('%d');

console.log('number', number);

REPL

>> 2015    
number 2015

Array return

var scanf = require('scanf');

var result = scanf('%s%d%d');

console.log('result', result);

REPL

>> Alan 24 180        
result [ 'Alan', 24, 180 ]

Json return

var scanf = require('scanf');

var result = scanf('%d %f %s %x %o', 'integer', 'float', 'string', 'hex', 'octal');

console.log('result', result);

REPL

>> 12 3.1415926 hello 1F 10    
result { 
  integer: 12,
  float: 3.1415926,
  string: 'hello',
  hex: 31,
  octal: 8 
}

sscanf

REPL

>> var sscanf = require('scanf').sscanf;
undefined
>> sscanf('12 34', '%d');
12
>> sscanf('Alan 20 180', '%s%d%d')
[ 'Alan', 20, 180 ]
>> sscanf('12 3.1415926 hello', '%d %f %s', 'month', 'pi', 'world');
{ month: 12, pi: 3.1415926, world: 'hello' }
>> sscanf('   14   ??  Ss     0:07.59 /usr/sbin/securityd -i', '%d %s %s %s %s %s', 'pid', 'tty', 'stat', 'time', 'exec', 'param');
{ pid: 14,
  tty: '??',
  stat: 'Ss',
  time: '0:07.59',
  exec: '/usr/sbin/securityd',
  param: '-i' }

you can see the ./tests files for more detail.

If there are some formats not support or go broken, you can contact author with email lellansin@gmail.com.

Contributors

License

MIT

1.2.0

1 year ago

1.1.2

4 years ago

1.1.1

5 years ago

1.1.0

5 years ago

1.0.2

7 years ago

1.0.1

7 years ago

1.0.0

7 years ago

0.7.3

8 years ago

0.7.2

9 years ago

0.7.0

9 years ago

0.6.4

9 years ago

0.6.3

9 years ago

0.6.2

9 years ago

0.6.1

10 years ago

0.6.0

10 years ago

0.5.1

10 years ago

0.5.0

10 years ago

0.4.2

10 years ago

0.4.1

10 years ago

0.4.0

10 years ago

0.3.1

10 years ago

0.3.0

10 years ago

0.2.0

10 years ago

0.1.1

10 years ago

0.1.0

10 years ago