From 60099be59ca97f2c934e59b3f9878c1ff944c90a Mon Sep 17 00:00:00 2001 From: Cam <21029087+csqrl@users.noreply.github.com> Date: Mon, 5 Dec 2022 20:02:46 +0000 Subject: [PATCH] Implement `Array.is` (#8) --- .prettierrc.json | 14 ++++++++++++++ .stylua.toml | 7 +++++++ .vscode/settings.json | 25 +++++++++++++++++++++++++ README.md | 1 + package-lock.json | 4 ++-- package.json | 2 +- src/Array.d.ts | 5 ++++- src/Array/init.lua | 2 ++ src/Array/is.lua | 25 +++++++++++++++++++++++++ src/Array/is.spec.lua | 11 +++++++++++ src/Dictionary.d.ts | 2 +- src/Set.d.ts | 2 +- src/index.d.ts | 2 +- wally.toml | 2 +- 14 files changed, 96 insertions(+), 8 deletions(-) create mode 100644 .prettierrc.json create mode 100644 .stylua.toml create mode 100644 .vscode/settings.json create mode 100644 src/Array/is.lua create mode 100644 src/Array/is.spec.lua diff --git a/.prettierrc.json b/.prettierrc.json new file mode 100644 index 0000000..db44278 --- /dev/null +++ b/.prettierrc.json @@ -0,0 +1,14 @@ +{ + "arrowParens": "always", + "bracketSameLine": false, + "bracketSpacing": true, + "endOfLine": "auto", + "jsxSingleQuote": false, + "printWidth": 80, + "quoteProps": "consistent", + "semi": false, + "singleQuote": false, + "tabWidth": 2, + "trailingComma": "es5", + "useTabs": false +} diff --git a/.stylua.toml b/.stylua.toml new file mode 100644 index 0000000..3ae7657 --- /dev/null +++ b/.stylua.toml @@ -0,0 +1,7 @@ +column_width = 100 +line_endings = "Unix" +indent_type = "Tabs" +indent_width = 4 +quote_style = "AutoPreferDouble" +call_parentheses = "Always" +collapse_simple_statement = "Never" diff --git a/.vscode/settings.json b/.vscode/settings.json new file mode 100644 index 0000000..4c1c7a3 --- /dev/null +++ b/.vscode/settings.json @@ -0,0 +1,25 @@ +{ + "robloxLsp.diagnostics.severity": { + "unused-local": "Error", + "unused-function": "Error", + "unused-vararg": "Error", + "duplicate-index": "Error" + }, + "[lua]": { + "editor.defaultFormatter": "JohnnyMorganz.stylua" + }, + "[json]": { + "editor.defaultFormatter": "esbenp.prettier-vscode" + }, + "[typescript]": { + "editor.defaultFormatter": "esbenp.prettier-vscode" + }, + "editor.codeActionsOnSave": { + "source.fixAll": true, + "source.organizeImports": true + }, + "editor.formatOnSave": true, + "editor.formatOnPaste": true, + "editor.formatOnType": true, + "editor.formatOnSaveMode": "modifications" +} diff --git a/README.md b/README.md index d15f920..7d09dee 100644 --- a/README.md +++ b/README.md @@ -109,6 +109,7 @@ As per the recommendations in [Llama's README][freddylist/llama], the following - `at`: Get an element at a specific index (negative indices are supported). - `freeze`: Freeze an array. - `freezeDeep`: Freeze an array and all nested arrays. +- `is`: Check if the passed value is an array. - `shuffle`: Shuffle the elements of an array to a random order. ### Dictionaries diff --git a/package-lock.json b/package-lock.json index 4f8c750..269b3f0 100644 --- a/package-lock.json +++ b/package-lock.json @@ -1,12 +1,12 @@ { "name": "@rbxts/sift", - "version": "0.0.2", + "version": "0.0.4", "lockfileVersion": 2, "requires": true, "packages": { "": { "name": "@rbxts/sift", - "version": "0.0.2", + "version": "0.0.4", "license": "MIT", "devDependencies": { "@rbxts/compiler-types": "^1.2.3-types.0", diff --git a/package.json b/package.json index 96aadd9..f4ebe01 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "@rbxts/sift", - "version": "0.0.3", + "version": "0.0.4", "description": "Immutable data library for Luau", "main": "out/init.lua", "types": "out/index.d.ts", diff --git a/src/Array.d.ts b/src/Array.d.ts index 3044c4d..451c409 100644 --- a/src/Array.d.ts +++ b/src/Array.d.ts @@ -1,4 +1,4 @@ -import type { ObjectKey, ReadonlyDeep, ReplaceType, SiftNone } from "Util" +import type { ReadonlyDeep, ReplaceType, SiftNone } from "./Util" declare namespace SiftArray { export function at( @@ -71,6 +71,8 @@ declare namespace SiftArray { export function insert(array: T[], index: number, ...values: T[]): T[] + export function is(value: any): boolean + export function last(array: T[]): T export function map( @@ -174,6 +176,7 @@ declare namespace SiftArray { includes as has, push as append, unshift as prepend, + is as isArray, } } diff --git a/src/Array/init.lua b/src/Array/init.lua index 8ecaf33..d68a4b2 100644 --- a/src/Array/init.lua +++ b/src/Array/init.lua @@ -40,6 +40,7 @@ local Array = { freezeDeep = require(script.freezeDeep), includes = require(script.includes), insert = require(script.insert), + is = require(script.is), last = require(script.last), map = require(script.map), pop = require(script.pop), @@ -74,5 +75,6 @@ Array.prepend = Array.unshift Array.indexOf = Array.find Array.has = Array.includes Array.contains = Array.includes +Array.isArray = Array.is return Array diff --git a/src/Array/is.lua b/src/Array/is.lua new file mode 100644 index 0000000..7a90d38 --- /dev/null +++ b/src/Array/is.lua @@ -0,0 +1,25 @@ +--!strict +--[=[ + @function is + @within Array + + @param object any -- The object to check. + @return boolean -- Whether the object is an array. + + Checks if the given object is an array. + + ```lua + local array = { 1, 2, 3 } + local dictionary = { hello = "world" } + local mixed = { 1, 2, hello = "world" } + + Array.is(array) -- true + Array.is(dictionary) -- false + Array.is(mixed) -- false + ``` +]=] +local function is(object: any): boolean + return typeof(object) == "table" and #object > 0 and next(object, #object) == nil +end + +return is diff --git a/src/Array/is.spec.lua b/src/Array/is.spec.lua new file mode 100644 index 0000000..aa94cbd --- /dev/null +++ b/src/Array/is.spec.lua @@ -0,0 +1,11 @@ +return function() + local isArray = require(script.Parent.is) + + it("should return whether the given object is an array", function() + expect(isArray({})).to.equal(false) + expect(isArray({ 1, 2, 3 })).to.equal(true) + expect(isArray({ hello = "world" })).to.equal(false) + expect(isArray({ 1, 2, hello = "world" })).to.equal(false) + expect(isArray({ 1, 2, 3, nil, 5 })).to.equal(true) + end) +end diff --git a/src/Dictionary.d.ts b/src/Dictionary.d.ts index 1235355..d7b0a27 100644 --- a/src/Dictionary.d.ts +++ b/src/Dictionary.d.ts @@ -5,7 +5,7 @@ import { ObjectKey, ReadonlyDeep, TryIndex, -} from "Util" +} from "./Util" declare namespace SiftDictionary { export function copy(dictionary: T): T diff --git a/src/Set.d.ts b/src/Set.d.ts index 98e2532..b4a6122 100644 --- a/src/Set.d.ts +++ b/src/Set.d.ts @@ -1,4 +1,4 @@ -import type { AnySet } from "Util" +import type { AnySet } from "./Util" declare namespace SiftSet { export function add(set: Set, ...values: I[]): Set diff --git a/src/index.d.ts b/src/index.d.ts index b65f4fa..05946dc 100644 --- a/src/index.d.ts +++ b/src/index.d.ts @@ -1,4 +1,4 @@ -import type { SiftNone } from "Util" +import type { SiftNone } from "./Util" import SiftArray from "./Array" import SiftDictionary from "./Dictionary" diff --git a/wally.toml b/wally.toml index 5ca4ac9..536b16f 100644 --- a/wally.toml +++ b/wally.toml @@ -1,7 +1,7 @@ [package] name = "csqrl/sift" description = "Immutable data library for Luau" -version = "0.0.3" +version = "0.0.4" license = "MIT" author = "csqrl (https://csqrl.dev)" registry = "https://github.com/upliftgames/wally-index"