Skip to content

Commit

Permalink
array
Browse files Browse the repository at this point in the history
  • Loading branch information
TodePond committed Feb 13, 2021
1 parent e69ec01 commit 94c7d27
Show file tree
Hide file tree
Showing 4 changed files with 263 additions and 1 deletion.
87 changes: 87 additions & 0 deletions build/habitat-embed.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,93 @@ const Habitat = {}
{

const install = (global) => {

Reflect.defineProperty(Array.prototype, "last", {
get() {
return this[this.length-1]
},
set(value) {
Reflect.defineProperty(this, "last", {value, configurable: true, writable: true, enumerable: true})
}
}, {configurable: true, enumerable: false, writable: true})

Reflect.defineProperty(Array.prototype, "first", {
get() {
return this[0]
},
set(value) {
Reflect.defineProperty(this, "first", {value, configurable: true, writable: true, enumerable: true})
}
}, {configurable: true, enumerable: false, writable: true})

Reflect.defineProperty(Array.prototype, "clone", {
get() {
return [...this]
},
set(value) {
Reflect.defineProperty(this, "clone", {value, configurable: true, writable: true, enumerable: true})
}
}, {configurable: true, enumerable: false, writable: true})

Reflect.defineProperty(Array.prototype, "at", {
value(position) {
if (position >= 0) return this[position]
return this[this.length + position]
}
}, {configurable: true, enumerable: false, writable: true})

Reflect.defineProperty(Array.prototype, "shuffle", {
value() {
for (let i = this.length - 1; i > 0; i--) {
const r = Math.floor(Math.random() * (i+1))
;[this[i], this[r]] = [this[r], this[i]]
}
return this
}
}, {configurable: true, enumerable: false, writable: true})

Reflect.defineProperty(Array.prototype, "trim", {
value() {
if (this.length == 0) return this
let start = this.length - 1
let end = 0
for (let i = 0; i < this.length; i++) {
const value = this[i]
if (value !== undefined) {
start = i
break
}
}
for (let i = this.length - 1; i >= 0; i--) {
const value = this[i]
if (value !== undefined) {
end = i + 1
break
}
}
this.splice(end)
this.splice(0, start)
return this
}
}, {configurable: true, enumerable: false, writable: true})

Reflect.defineProperty(Array.prototype, "repeat", {
value(n) {
if (n === 0) {
this.splice(0)
return this
}
if (n < 0) {
this.reverse()
n = n - n - n
}
const clone = [...this]
for (let i = 1; i < n; i++) {
this.push(...clone)
}
return this
}
}, {configurable: true, enumerable: false, writable: true})

}

Expand Down
87 changes: 87 additions & 0 deletions build/habitat-import.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,93 @@ const Habitat = {}
{

const install = (global) => {

Reflect.defineProperty(Array.prototype, "last", {
get() {
return this[this.length-1]
},
set(value) {
Reflect.defineProperty(this, "last", {value, configurable: true, writable: true, enumerable: true})
}
}, {configurable: true, enumerable: false, writable: true})

Reflect.defineProperty(Array.prototype, "first", {
get() {
return this[0]
},
set(value) {
Reflect.defineProperty(this, "first", {value, configurable: true, writable: true, enumerable: true})
}
}, {configurable: true, enumerable: false, writable: true})

Reflect.defineProperty(Array.prototype, "clone", {
get() {
return [...this]
},
set(value) {
Reflect.defineProperty(this, "clone", {value, configurable: true, writable: true, enumerable: true})
}
}, {configurable: true, enumerable: false, writable: true})

Reflect.defineProperty(Array.prototype, "at", {
value(position) {
if (position >= 0) return this[position]
return this[this.length + position]
}
}, {configurable: true, enumerable: false, writable: true})

Reflect.defineProperty(Array.prototype, "shuffle", {
value() {
for (let i = this.length - 1; i > 0; i--) {
const r = Math.floor(Math.random() * (i+1))
;[this[i], this[r]] = [this[r], this[i]]
}
return this
}
}, {configurable: true, enumerable: false, writable: true})

Reflect.defineProperty(Array.prototype, "trim", {
value() {
if (this.length == 0) return this
let start = this.length - 1
let end = 0
for (let i = 0; i < this.length; i++) {
const value = this[i]
if (value !== undefined) {
start = i
break
}
}
for (let i = this.length - 1; i >= 0; i--) {
const value = this[i]
if (value !== undefined) {
end = i + 1
break
}
}
this.splice(end)
this.splice(0, start)
return this
}
}, {configurable: true, enumerable: false, writable: true})

Reflect.defineProperty(Array.prototype, "repeat", {
value(n) {
if (n === 0) {
this.splice(0)
return this
}
if (n < 0) {
this.reverse()
n = n - n - n
}
const clone = [...this]
for (let i = 1; i < n; i++) {
this.push(...clone)
}
return this
}
}, {configurable: true, enumerable: false, writable: true})

}

Expand Down
87 changes: 87 additions & 0 deletions source/array.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,93 @@
{

const install = (global) => {

Reflect.defineProperty(Array.prototype, "last", {
get() {
return this[this.length-1]
},
set(value) {
Reflect.defineProperty(this, "last", {value, configurable: true, writable: true, enumerable: true})
}
}, {configurable: true, enumerable: false, writable: true})

Reflect.defineProperty(Array.prototype, "first", {
get() {
return this[0]
},
set(value) {
Reflect.defineProperty(this, "first", {value, configurable: true, writable: true, enumerable: true})
}
}, {configurable: true, enumerable: false, writable: true})

Reflect.defineProperty(Array.prototype, "clone", {
get() {
return [...this]
},
set(value) {
Reflect.defineProperty(this, "clone", {value, configurable: true, writable: true, enumerable: true})
}
}, {configurable: true, enumerable: false, writable: true})

Reflect.defineProperty(Array.prototype, "at", {
value(position) {
if (position >= 0) return this[position]
return this[this.length + position]
}
}, {configurable: true, enumerable: false, writable: true})

Reflect.defineProperty(Array.prototype, "shuffle", {
value() {
for (let i = this.length - 1; i > 0; i--) {
const r = Math.floor(Math.random() * (i+1))
;[this[i], this[r]] = [this[r], this[i]]
}
return this
}
}, {configurable: true, enumerable: false, writable: true})

Reflect.defineProperty(Array.prototype, "trim", {
value() {
if (this.length == 0) return this
let start = this.length - 1
let end = 0
for (let i = 0; i < this.length; i++) {
const value = this[i]
if (value !== undefined) {
start = i
break
}
}
for (let i = this.length - 1; i >= 0; i--) {
const value = this[i]
if (value !== undefined) {
end = i + 1
break
}
}
this.splice(end)
this.splice(0, start)
return this
}
}, {configurable: true, enumerable: false, writable: true})

Reflect.defineProperty(Array.prototype, "repeat", {
value(n) {
if (n === 0) {
this.splice(0)
return this
}
if (n < 0) {
this.reverse()
n = n - n - n
}
const clone = [...this]
for (let i = 1; i < n; i++) {
this.push(...clone)
}
return this
}
}, {configurable: true, enumerable: false, writable: true})

}

Expand Down
3 changes: 2 additions & 1 deletion tinker/tinker.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
// Use this file to tinker with the project
Habitat.install(window)
const luke = {}
const luke = {name: "Luke", age: 27}
const scores = [2, 3, 5]

0 comments on commit 94c7d27

Please sign in to comment.