Giter VIP home page Giter VIP logo

allen's Introduction

Allen is a Lua library which provides a set of utilities and helpers for strings operations. In a nutshell, Allen is to strings what Moses is to tables. Allen can be considered as an extension of the built-in Lua string library.
Allen's API was heavily inspired by the Javascript library Underscore.string.

##Installation## Put the file Allen.lua inside your project folder and call it using require command. It will return a table containing a set of functions.

##Usage example##

local lyrics = 'hey I just met you\nand this is crazy\nbut here is my number\nso call me maybe'
lyrics = _.lines(lyrics)
for i,line in ipairs(lyrics) do 
  line = _.capitalizeFirst(line) 
end
-- => { 'Hey I just met you',
--      'And this is crazy',
--      'But here is my number',
--      'So call me maybe'}

##Allen and the built-in string library## Allen provides a very handy function named _.import, which links Lua's native string library with Allen's functions.

_.import()
print(string.capitalize('hello'))
print(('Hello'):splice(2,2,'r'))

##About Lua Operators## Once Allen is called inside your projet,it enables the use of arithmetic operators on strings by default.
Compatible operators are : +,*,/,%,^,-.

  • Addition operator (+) : concatenates two strings
  • Unary operator (-) : reverses a string
  • Multiplication operator (*) : repeats a string
  • Power operator (^) : also repeats a string
  • Division operator (/) : used for chopping strings (dividing it in subsets strings)
  • Modulo operator (%) : returns the remainder subset string after a chopping process.

##Strings Indexing## Using Allen, strings can be indexed, like regular Lua tables.

for i = 1,5 do 
  print(('Accessing %s[%d] :'):format('Hello',i),('Hello')[i]) 
end
-- => Accessing Hello[1] :	H
-- => Accessing Hello[2] :	e
-- => Accessing Hello[3] :	l
-- => Accessing Hello[4] :	l
-- => Accessing Hello[5] :	o

##Strings as functions## Once Allen is loaded inside a project, strings can be called as functions.

local s = 'Hello'
print(s()) --> 'Hello'

Passing a positive number i as the first argument will return the character at the corresponding index

local s = 'Hello'
print(s(2)) --> 'e'

Passing a number j as a second argument will return the substring between i and j

local s = 'Hello'
print(s(2,4)) --> 'ell'

Passing a character as a second argument will replace the character at index i

local s = 'Helo'
print(s(3,'r')) --> 'Hero'

See Allen_StringOp_Snippets.lua for more details.

##API Overview## Allen offers a lot of functions, iterators to operate on strings.
A fully complete documentation, with usage examples can be found here : Documentation
Some usage examples can be found here : Allen_Lib_Snippets.lua and Allen_StringOp_Snippets.lua

##Credits and Thanks## Esa-Matti Suuronen and Edward Tsech, for the amazing Underscore.string

##License## This work is under MIT-LICENSE
Copyright (c) 2012 Roland Yonaba

Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

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.