From 7b5aa05b026aadab237d9d101a9f455ea070e5e8 Mon Sep 17 00:00:00 2001 From: Kim Alvefur Date: Wed, 29 Mar 2017 22:38:31 +0200 Subject: util.id: New util for producing random identifiers of varying sizes --- util/id.lua | 26 ++++++++++++++++++++++++++ 1 file changed, 26 insertions(+) create mode 100644 util/id.lua (limited to 'util') diff --git a/util/id.lua b/util/id.lua new file mode 100644 index 00000000..731355fa --- /dev/null +++ b/util/id.lua @@ -0,0 +1,26 @@ +-- Prosody IM +-- Copyright (C) 2008-2017 Matthew Wild +-- Copyright (C) 2008-2017 Waqas Hussain +-- Copyright (C) 2008-2017 Kim Alvefur +-- +-- This project is MIT/X11 licensed. Please see the +-- COPYING file in the source package for more information. +-- + +local s_gsub = string.gsub; +local random_bytes = require "util.random".bytes; +local base64_encode = require "util.encodings".base64.encode; + +local b64url = { ["+"] = "-", ["/"] = "_", ["="] = "" }; +local function b64url_random(len) + return (s_gsub(base64_encode(random_bytes(len)), "[+/=]", b64url)); +end + +return { + short = function () return b64url_random(6); end; + medium = function () return b64url_random(12); end; + long = function () return b64url_random(24); end; + custom = function (size) + return function () return b64url_random(size); end; + end; +} -- cgit v1.2.3 From 72018f7be717db4e99cf175d46cbc9d97c6249aa Mon Sep 17 00:00:00 2001 From: Kim Alvefur Date: Fri, 31 Mar 2017 17:29:08 +0200 Subject: util.array: Add method for filtering out duplicate values --- util/array.lua | 12 ++++++++++++ 1 file changed, 12 insertions(+) (limited to 'util') diff --git a/util/array.lua b/util/array.lua index 05fa97ca..150b4355 100644 --- a/util/array.lua +++ b/util/array.lua @@ -92,6 +92,18 @@ function array_base.sort(outa, ina, ...) return outa; end +function array_base.unique(outa, ina) + local seen = {}; + return array_base.filter(outa, ina, function (item) + if seen[item] then + return false; + else + seen[item] = true; + return true; + end + end); +end + function array_base.pluck(outa, ina, key) for i = 1, #ina do outa[i] = ina[i][key]; -- cgit v1.2.3