GithubHelp home page GithubHelp logo

wires.lua's Introduction

wires.lua 🔌

An assortment of useful string functions ⚡

Installation

Luarocks

If you are using luarocks, just run:

luarocks install wires

Manual

Copy the wires.lua file somewhere where your Lua interpreter will be able to find it and require it accordingly:

local wires = require("wires")

Tips

You can extend the Lua string namespace by doing:

for k, v in pairs(wires) do 
    string[k] = v 
end

so you can use them as string methods:

(""):trim()

or

string.trim("")

Interface

wires.starts(s, prefix) Find if string s starts with prefix

wires.starts("foo bar baz", "foo") -> true

Arguments:

  • s (string) - String to match
  • prefix (string) - String to match in s

Returns:

  • (boolean) - true if s starts with prefix, false if not

wires.ends(s, suffix) Find if string s ends with suffix

wires.ends("foo bar baz", "baz") -> true

Arguments:

  • s (string) - String to match
  • suffix (string) - String to match in s

Returns:

  • (boolean) - true if s ends with suffix, false if not

wires.trim(s) Remove whitespace (defined as Lua pattern "%s") from the beginning and end of a string

wires.trim(" foo ") -> "foo"

Arguments:

  • s (string) - String to trim

Returns:

  • (string) - The trimmed string

wires.escape(s[, mode]) Escape magic characters of s so that it can be used as a pattern. The optional argument mode may be "*i" (for case insensitive)

wires.escape("%s", "*i") -> "%%[sS]"

Arguments:

  • s (string) - String to escape
  • mode (string) - String for whether the escape should be case insensetive

Returns:

  • (string) - The escaped string

wires.gsplit(s, sep[, start[, plain]]) -> iter() -> e[, captures...] Split s by a separator pattern (or plain string) and iterate over the elements

for c in wires.gsplit('foo bar baz', '%s') do
   print(c)
end

Arguments:

  • s (string) - string to gsplit
  • sep (string) - pattern to gsplit s
  • start (number) - Capture to begin at
  • plain (boolean) - Whether to match patterns or not

Returns:

  • c (string) - A capture from s

wires.lines(s[, opt]) -> iter() -> s Iterate the lines of a string, The lines are split at \r\n, \r and \n markers

for c in wires.lines("foo\nbar\nbaz") do
    print(c)
end

Arguments:

  • s (string) - String with lines to iterate over
  • opt (string) - *L (include line endings; default) or *l (exclude).

Returns:

  • c (string) - The captured line

wires.tohex(s, upper) Convert a hex string to its binary representation

wires.tohex("foo") -> "666f6f"

Arguments:

  • s (string) - String to convert to it's hex representation
  • upper (boolean) - Whether alphabetical characters should be capitalised

Returns:

  • (string) - Hex representation of s

wires.fromhex(s) Convert a hex string to its binary representation

wires.fromhex("666f6f") -> "foo"

Arguments:

  • s (string) - Hex string to convert to it's binary represenation

Returns:

  • (string) - Binary representation of s

License

This library is free software; You may redistribute and/or modify it under the terms of the MIT license. See LICENSE for details.

wires.lua's People

Contributors

kitsunies avatar

Stargazers

 avatar 涵曦 avatar  avatar li5414 avatar André L. Alvares avatar chris avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.