Skip to content

Commit

Permalink
Implement Array.is (#8)
Browse files Browse the repository at this point in the history
  • Loading branch information
cxmeel authored Dec 5, 2022
1 parent 4767957 commit 60099be
Show file tree
Hide file tree
Showing 14 changed files with 96 additions and 8 deletions.
14 changes: 14 additions & 0 deletions .prettierrc.json
Original file line number Diff line number Diff line change
@@ -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
}
7 changes: 7 additions & 0 deletions .stylua.toml
Original file line number Diff line number Diff line change
@@ -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"
25 changes: 25 additions & 0 deletions .vscode/settings.json
Original file line number Diff line number Diff line change
@@ -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"
}
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
4 changes: 2 additions & 2 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -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",
Expand Down
5 changes: 4 additions & 1 deletion src/Array.d.ts
Original file line number Diff line number Diff line change
@@ -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<V extends unknown>(
Expand Down Expand Up @@ -71,6 +71,8 @@ declare namespace SiftArray {

export function insert<T>(array: T[], index: number, ...values: T[]): T[]

export function is(value: any): boolean

export function last<T>(array: T[]): T

export function map<T, U extends T>(
Expand Down Expand Up @@ -174,6 +176,7 @@ declare namespace SiftArray {
includes as has,
push as append,
unshift as prepend,
is as isArray,
}
}

Expand Down
2 changes: 2 additions & 0 deletions src/Array/init.lua
Original file line number Diff line number Diff line change
Expand Up @@ -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),
Expand Down Expand Up @@ -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
25 changes: 25 additions & 0 deletions src/Array/is.lua
Original file line number Diff line number Diff line change
@@ -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
11 changes: 11 additions & 0 deletions src/Array/is.spec.lua
Original file line number Diff line number Diff line change
@@ -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
2 changes: 1 addition & 1 deletion src/Dictionary.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import {
ObjectKey,
ReadonlyDeep,
TryIndex,
} from "Util"
} from "./Util"

declare namespace SiftDictionary {
export function copy<T extends object>(dictionary: T): T
Expand Down
2 changes: 1 addition & 1 deletion src/Set.d.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import type { AnySet } from "Util"
import type { AnySet } from "./Util"

declare namespace SiftSet {
export function add<T, I>(set: Set<T>, ...values: I[]): Set<T | I>
Expand Down
2 changes: 1 addition & 1 deletion src/index.d.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import type { SiftNone } from "Util"
import type { SiftNone } from "./Util"

import SiftArray from "./Array"
import SiftDictionary from "./Dictionary"
Expand Down
2 changes: 1 addition & 1 deletion wally.toml
Original file line number Diff line number Diff line change
@@ -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"
Expand Down

0 comments on commit 60099be

Please sign in to comment.