Giter VIP home page Giter VIP logo

array.ahk's Introduction

Conversion of JavaScript's Array methods to AutoHotkey

npm docs license

AutoHotkey lacks built-in iteration helper methods to perform many of the common array behaviors found in other languages. This package ports most of JavaScript's Array object methods to AutoHotkey's Array object. Because Javascript is zero-based and AutoHotkey is not, there are some slight differences.

Ported Methods

  • concat
  • every
  • fill
  • filter
  • find
  • findIndex
  • forEach
  • includes
  • indexOf
  • join
  • lastIndexOf
  • map
  • reduce
  • reduceRight
  • reverse
  • shift
  • slice
  • some
  • sort
  • splice
  • toString
  • unshift
  • values

Installation

In a terminal or command line navigated to your project folder:

npm install array.ahk

You may also review or copy the library from ./export.ahk on GitHub

In your code only export.ahk needs to be included:

#Include %A_ScriptDir%\node_modules
#Include array.ahk\export.ahk

msgbox, % [1,2,3].join()
; => "1,2,3"

You may also review or copy the library from ./export.ahk on GitHub; #Incude as you would normally when manually downloading.

API

The package modifies the Array object when #Included near the top of your script.

Array.<fn>([params*])

; Map to doubled value
arrayInt := [1, 5, 10]
arrayInt.map(func("fn_doubleInt"))
; => [2, 10, 20]

fn_doubleInt(int) {
	return int * 2
}


; Map to object property
arrayObj := [{"name": "bob", "age": 22}, {"name": "tom", "age": 51}]
arrayObj.map(func("fn_returnName"))
; => ["bob", "tom"]

fn_returnName(obj) {
	return obj.name
}


; Method chaining
arrayObj := [{"name": "bob", "age": 22}, {"name": "tom", "age": 51}]
msgbox, % arrayObj.map(func("fn_returnProp").bind("age"))
	.map(func("fn_doubleInt"))
	.join(",")
; => "44,102"

fn_returnProp(prop, obj) {
	return obj[prop]
}

Sorting

JavaScript does not use start/end or left/right parameters, and this implementation follows the same approach.

array.sort([params*])

[11,9,5,10,1,6,3,4,7,8,2].sort()
; => [1,2,3,4,5,6,7,8,9,10,11]


[11,9,5,10,1,6,3,4,7,8,2].sort(func("fn_reverse"))
fn_reverse(a, b) {
	return b - a
}
; => [11, 10, 9, 8, 7, 6, 5, 4, 3, 2, 1])

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.