0.1.3 • Published 8 years ago

xecio-util v0.1.3

Weekly downloads
2
License
MIT
Repository
github
Last release
8 years ago

xecio-util

Utilities for Xecio.

Installation

$ npm install xecio-util --save

Usage

var util = require('xecio-util');

CacheStream()

Caches contents piped to the stream.

var stream = new CacheStream();

fs.createReadStream('/path/to/file').pipe(stream);

stream.on('finish', function(){
  // Read cache piped to the stream
  console.log(stream.getCache());

  // Destroy cache
  stream.destroy();
});

camelCaseKeys(obj, options)

Convert object keys to camelCase. Original keys will be converted to getter/setter and sync to the camelCase keys.

camelCaseKeys({
  foo_bar: 'test'
});
// { fooBar: 'test', foo_bar: 'test' }

escapeRegex(str)

Escapes special characters in a regular expression.

escapeDiacritic(str)

Escapes diacritic characters in a string.

escapeHTML(str)

Escapes HTML entities in a string.

hash(str)

Generates SHA1 hash.

hash('123456');
// <Buffer 7c 4a 8d 09 ca 37 62 af 61 e5 95 20 94 3d c2 64 94 f8 94 1b>

HashStream()

Generates SHA1 hash with a transform stream.

var stream = new HashStream();

fs.createReadStream('/path/to/file')
  .pipe(stream)
  .on('finish', function(){
    console.log(stream.read());
  });

highlight(str, options)

Syntax highlighting for a code block.

OptionDescriptionDefault
gutterWhether to show line numberstrue
wrapWhether to wrap the code blocktrue
firstLineFirst line number1
langLanguage
captionCaption
tabReplace tabs
autoDetectDetect language automaticallyfalse

htmlTag(tag, attrs, text)

Creates a html tag.

htmlTag('img', {src: 'example.png'})
// <img src="example.png">

htmlTag('a', {href: 'http://xec.io/'}, 'Xecio')
// <a href="http://xec.io/">Xecio</a>

Pattern(rule)

Parses the string and tests if the string matches the rule. rule can be a string, a regular expression or a function.

var pattern = new Pattern('posts/:id');

pattern.match('posts/89');
// {0: 'posts/89', 1: '89', id: '89'}
var pattern = new Pattern('posts/*path');

pattern.match('posts/2013/hello-world');
// {0: 'posts/2013/hello-world', 1: '2013/hello-world', path: '2013/hello-world'}

Permalink(rule, options)

Parses a permalink.

OptionDescription
segmentsCustomize the rule of a segment in the permalink
var permalink = new Permalink(':year/:month/:day/:title', {
  segments: {
    year: /(\d{4})/,
    month: /(\d{2})/,
    day: /(\d{2})/
  }
});

permalink.parse('2014/01/31/test');
// {year: '2014', month: '01', day: '31', title: 'test'}

permalink.test('2014/01/31/test');
// true

permalink.stringify({year: '2014', month: '01', day: '31', title: 'test'})
// 2014/01/31/test

slugize(str, options)

Transforms a string into a clean URL-friendly string.

OptionDescriptionDefault
separatorSeparator-
transformTransform the string into lower case (1) or upper case (2)
slugize('Hello World') = 'Hello-World'
slugize('Hellô Wòrld') = 'Hello-World'
slugize('Hello World', {separator: '_'}) = 'Hello_World'
slugize('Hello World', {transform: 1}) = 'hello-world'
slugize('Hello World', {transform: 2}) = 'HELLO-WORLD'

spawn(command, args, options)

Launches a new process with the given command. This method returns a promise.

OptionDescriptionDefault
cwdCurrent working directory of the child process
envEnvironment key-value pairs
stdioChild's stdio configuration
detachedThe child will be a process group leader
uidSets the user identity of the process
gidSets the group identity of the process
verboseDisplay messages on the consolefalse
encodingSets the encoding of the output stringutf8
spawn('cat', 'test.txt').then(function(content){
  console.log(content);
});

stripHTML(str)

Removes HTML tags in a string.

wordWrap(str, options)

Wraps the string no longer than line width. This method breaks on the first whitespace character that does not exceed line width.

OptionDescriptionDefault
widthLine width80
wordWrap('Once upon a time')
// Once upon a time

wordWrap('Once upon a time, in a kingdom called Far Far Away, a king fell ill, and finding a successor to the throne turned out to be more trouble than anyone could have imagined...')
// Once upon a time, in a kingdom called Far Far Away, a king fell ill, and finding\na successor to the throne turned out to be more trouble than anyone could have\nimagined...

wordWrap('Once upon a time', {width: 8})
// Once\nupon a\ntime

wordWrap('Once upon a time', {width: 1})
// Once\nupon\na\ntime

truncate(str, options)

Truncates a given text after a given length if text is longer than length. The last characters will be replaced with the omission option for a total length not exceeding length.

OptionDescriptionDefault
lengthMax length of the string30
omissionOmission text...
separatortruncate text at a natural break
truncate('Once upon a time in a world far far away')
// "Once upon a time in a world..."

truncate('Once upon a time in a world far far away', {length: 17})
// "Once upon a ti..."

truncate('Once upon a time in a world far far away', {length: 17, separator: ' '})
// "Once upon a..."

truncate('And they found that many people were sleeping better.', {length: 25, omission: '... (continued)'})
// "And they f... (continued)"

License

MIT