diff --git a/README.md b/README.md index a2a6f3d..ef81475 100644 --- a/README.md +++ b/README.md @@ -28,10 +28,14 @@ `Fork` 之后进入自己的项目里,然后修改项目名,再使用 `git` 工具拉取到本地,即可上手开发。 -开发时请注意编写区域,代码中有三块主要区域: **脚本使用者配置** 、 **框架核心代码** 以及 **脚本编写区**。开发时请看清注释提示。尽量不要改动 **框架核心代码**,**脚本使用者配置** 和 **脚本编写区** 可以自由修改。 +开发时请注意编写区域,代码中有三块主要区域: **脚本使用者配置** 、 **框架核心代码** 以及 **脚本编写区**。 + +请看清注释提示,尽量不要改动 **框架核心代码**。 + +**脚本使用者配置** 和 **脚本编写区** 可以自由修改。 ### 开发技巧 -在此脚本中,使用 `lmf.on` 来监听事件: +在此框架中,使用 `lmf.on` 来监听事件: ```Lua lmf.on("load", function (e) @@ -77,19 +81,20 @@ end ``` ### 参考文档 +* [标准 Lua 5.1 库](./docs/lua5.1.md) * [框架API(基于原生重写/重命名)](./docs/api.md) * [罗技宏框架内置方法](./docs/lmf.md) * [扩展方法(table篇)](./docs/table.md) * [扩展方法(string篇)](./docs/string.md) ### 许可声明 -* 本脚本使用 **MIT** 许可,允许商用,允许二次开发后出售。 +* 本框架脚本使用 **MIT** 许可,允许商用,允许二次开发后出售。 ### 交流群 * 欢迎加入技术交流QQ群:[![logitech 鼠标宏技术交流](https://github.com/kiccer/Soldier76/raw/master/static/img/group.png)](https://kiccer.github.io/Soldier76/static/join_group.html) ([768483124](https://kiccer.github.io/Soldier76/static/join_group.html)) ### 问题反馈 -* 使用脚本时有任何疑问,或脚本存在不足之处可以在 [`Issues`](https://github.com/kiccer/logitech-macro-frame/issues) 反馈给我 +* 使用框架时有任何疑问,或框架存在不足之处可以在 [`Issues`](https://github.com/kiccer/logitech-macro-frame/issues) 反馈给我 ### 谁使用了此框架 * 暂无... diff --git a/docs/lua5.1.md b/docs/lua5.1.md new file mode 100644 index 0000000..1395b83 --- /dev/null +++ b/docs/lua5.1.md @@ -0,0 +1,51 @@ +# 标准 Lua 5.1 库 + +以下函数为受支持的标准库函数: +* string.byte +* string.char +* string.dump +* string.find +* string.format +* string.gmatch +* string.gsub +* string.len +* string.lower +* string.match +* string.rep +* string.reverse +* string.sub +* string.upper +* table.concat +* table.insert +* table.maxn +* table.remove +* table.sort +* math.abs +* math.acos +* math.asin +* math.atan +* math.atan2 +* math.ceil +* math.cos +* math.deg +* math.exp +* math.floor +* math.fmod +* math.frexp +* math.huge +* math.ldexp +* math.log +* math.log10 +* math.max +* math.min +* math.modf +* math.pi +* math.pow +* math.rad +* math.random +* math.randomseed +* math.sin +* math.sinh +* math.sqrt +* math.tan +* math.tanh diff --git a/docs/table.md b/docs/table.md index d43f0d1..8eb83c8 100644 --- a/docs/table.md +++ b/docs/table.md @@ -12,6 +12,8 @@ * [table.indexOf](#tableindexOf) * [table.merge](#tablemerge) * [table.cloneDeep](#tablecloneDeep) +* [table.find](#tablefind) +* [table.filter](#tablefilter) ### **table.some** `table.some()` 方法判断数组中是否存在符合条件的项。 @@ -396,5 +398,81 @@ console.log(e, t) -- }, -- e = { 1, 2, 5, 3, 6, 8, 9, 5, 3 }, -- b = 2 +-- } +``` + +### **table.find** +`table.find()` 方法从数组中查找指定的项并返回该项 + +**参数列表** + +* table - 被查找的数组。 +* function - 查找方法。 + +**返回值** + +* any - 返回找到的项。 + +**备注信息** + +返回的为引用值。 + +**代码示范** + +```Lua +local t = { + { a = 1 }, + { a = 2 } +} + +local _t = table.find(t, function (n, i) + return n.a == 1 +end) + +console.log(_t) + +-- { +-- a = 1 +-- } +``` + +### **table.filter** +`table.filter()` 方法从数组中筛选符合条件的项 + +**参数列表** + +* table - 被筛选的数组。 +* function - 筛选方法。 + +**返回值** + +* table - 返回筛选后的列表。 + +**备注信息** + +返回的表中每一项皆为引用值。 + +**代码示范** + +```Lua +local t = { + { a = 1 }, + { a = 2 }, + { a = 3 } +} + +local _t = table.filter(t, function (n, i) + return n.a < 3 +end) + +console.log(_t) + +-- { +-- { +-- a = 1 +-- }, +-- { +-- a = 2 +-- } -- } ``` diff --git a/lmf.lua b/lmf.lua index 07893c9..cc41bc9 100644 --- a/lmf.lua +++ b/lmf.lua @@ -213,6 +213,28 @@ function string.split (str, s) return res end +-- table find +function table.find (t, f) + local res = nil + for i = 1, #t do + local n = t[i] + if f(n, i) then + res = n + break + end + end + return res +end + +-- table filter +function table.filter (t, f) + local res = {} + table.forEach(t, function (n, i) + if f(n, i) then table.push(res, n) end + end) + return res +end + -- join function function string.join (t, s) return table.reduce(t, function(n, m)