Using UTF-8-Validate

172 views
Skip to first unread message

Bryon Trott

unread,
Apr 21, 2016, 4:22:52 PM4/21/16
to nodejs
Hello,
I am new to node.js  I am trying to use the module UTF-8-Validate to check a simple test buffer.

However I am not sure how to call the function.


'use strict';
 
var isValid = require('utf-8-validate').isValidUTF8;

The module exposes 1 function:

isValidUTF8

Validate if the passed in buffer contains valid UTF-8 chars.

bu.isValidUTF8(buffer);


If anybody know how to do this or can point me to some sample code it would be very helpful.

Thanks

Matt

unread,
Apr 22, 2016, 12:46:24 AM4/22/16
to nod...@googlegroups.com
In the above example, you just use: if (isValid(buf)) { ... }


--
Job board: http://jobs.nodejs.org/
New group rules: https://gist.github.com/othiym23/9886289#file-moderation-policy-md
Old group rules: https://github.com/joyent/node/wiki/Mailing-List-Posting-Guidelines
---
You received this message because you are subscribed to the Google Groups "nodejs" group.
To unsubscribe from this group and stop receiving emails from it, send an email to nodejs+un...@googlegroups.com.
To post to this group, send email to nod...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/nodejs/01a2b4b9-961a-4f2d-b6cb-3021ef8dec89%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Bryon Trott

unread,
Apr 25, 2016, 5:27:33 PM4/25/16
to nodejs
I tried that and still get an error that says isValid is not a function?

C:\nodejs>node buftest.js
buffer
C:\nodejs\buftest.js:9
if (isValid(buf1)){
    ^

TypeError: isValid is not a function
    at Object.<anonymous> (C:\nodejs\buftest.js:9:5)
    at Module._compile (module.js:409:26)
    at Object.Module._extensions..js (module.js:416:10)
    at Module.load (module.js:343:32)
    at Function.Module._load (module.js:300:12)
    at Function.Module.runMain (module.js:441:10)
    at startup (node.js:139:18)
    at node.js:968:3

'use strict';
var isValid = require('utf-8-validate').isValidUTF8;
const buf1 = new Buffer('buffer');

console.log(buf1.toString());

if (isValid(buf1)){
 console.log('true')
 
}
else {
 console.log('false')
 
};

Matt

unread,
Apr 25, 2016, 7:24:02 PM4/25/16
to nod...@googlegroups.com
I downloaded it myself and dug into it. Seems the require line needs to be:

    var isValid = require('utf-8-validate').Validation.isValidUTF8;

Hope that helps.

Bryon Trott

unread,
Apr 26, 2016, 12:33:52 PM4/26/16
to nodejs
Yes, that helped.  The function now works.
Reply all
Reply to author
Forward
0 new messages