A collection of classes and functions that are used anywhere in Planck framework, while is not precisely it’s core.
npm install @takram/planck-core
Namespace
is a function similar to the technique described in Private Properties except this uses object literal rather than WeakMap, which will be an overkill in most cases. Another advantage that the article doesn’t mention is that using this technique over closure makes it easier to inspect its scope via this
.
import { Namespace } from '@takram/planck-core'
// Human-readable name is optional. 'T' should be displayed in the inspector of
// your browser as a property of the instance.
const internal = Namespace('T')
export default class T {
constructor(value) {
const scope = internal(this)
scope.value = value
}
get value() {
const scope = internal(this)
return scope.value
}
}
Aggregate
proxies aggregates the calls to its targets as well as to the functions of the targets.
Getting a property from an aggregate returns the properties of the first target. Setting a property propagates it to all the targets, resulting the targets should have the same value. Calling a function property (typically a method of instances) applies to all of them and returns an array of their results.
import { Aggregate } from '@takram/planck-core'
const a = { value: 'a', func() { return 'a' } }
const b = { value: 'b', func() { return 'b' } }
const aggregate = Aggregate.new(a, b)
aggregate.value = 'c'
console.log(a.value) // 'c'
console.log(b.value) // 'c'
console.log(aggregate.func()) // ['a', 'b']
Global
distinguishes browsers, workers and node processes by isBrowser
, isWorker
and isNode
respectively, and provides unified way to access the global scopes via scope
.
import { Global } from '@takram/planck-core'
if (Global.isNode) {
Global.scope.module = require('module')
} else if (Global.isWorker) {
Global.scope.module = importScripts('/js/module.js')
}
// Assume the module is already loaded in browser
Global.scope.module()
The example below limits simultaneous requests by the function load
to 10.
import { Semaphore } from '@takram/planck-core'
const semaphore = new Semaphore(10)
function load(url) {
// Unlike Promise, the function passed as the argument of “wait” will not be
// executed until the semaphore becomes available.
return semaphore.wait((resolve, reject) => {
const request = new XMLHttpRequest()
request.open('get', url)
request.onload = () => {
if (request.status !== 200) {
reject(request.status)
} else {
resolve(request.response)
}
}
request.onerror = reject
request.send()
})
}
[/* a bunch of urls */].map(url => load(url))
# Aggregate(target1 [, target2 [, ...]])
# aggregate[property]
# aggregate[property] = value
# aggregate([arg1 [, arg2 [, ...]]])
# Array.min(array, transform)
# Array.max(array, transform)
# new AssertionError([message])
# FilePath.resolve([path1 [, path2 [, ...]]])
# FilePath.normalize(path)
# FilePath.join([path1 [, path2 [, ...]]])
# FilePath.relative(from, to)
# FilePath.dirname(path)
# FilePath.basename(path [, ext])
# FilePath.extname(path)
# FilePath.sep
# FilePath.delimiter
# Global.isBrowser
# Global.isWorker
# Global.isNode
# Global.scope
# Hash(object)
# new ImplementationError([message])
# Math.lerp(start, stop, amount)
# Math.constrain(x, min, max)
# Math.map(x, min1, max1, min2, max2)
# Math.wrap(x, min, max)
# Math.radians(degrees)
# Math.degrees(radians)
# Math.sin(angle)
# Math.cos(angle)
# Math.tan(angle)
# Math.asin(x)
# Math.acos(x)
# Math.atan(y [, x])
# Math.pow(x, y)
# Math.exp(x)
# Math.log(x)
# Math.exp2(x)
# Math.log2(x)
# Math.sqrt(x)
# Math.inversesqrt(x)
# Math.abs(x)
# Math.sign(x)
# Math.floor(x)
# Math.ceil(x)
# Math.fract(x)
# Math.mod(x, y)
# Math.min(x, y)
# Math.max(x, y)
# Math.clamp(x, min, max)
# Math.mix(x, y, a)
# Math.step(edge, x)
# Math.smoothstep(edge0, edge1, x)
# Math.length(x)
# Math.distance(x, y)
# Math.dot(x, y)
# Math.cross(x, y)
# Math.normalize(x)
# Math.faceforward(N, I [, Nref])
# Math.reflect(I, N)
# Math.refract(I, N, eta)
# Namespace([name])
# namespace(object [, init])
#
Request.text(url [, options])
#
Request.text(options)
#
Request.json(url [, options])
#
Request.json(options)
#
Request.buffer(url [, options])
#
Request.buffer(options)
#
Request.csv(url [, options])
#
Request.csv(options)
#
Request.tsv(url [, options])
#
Request.tsv(options)
# request.abort()
# new Semaphore(capacity)
# semaphore.wait(callback)
# semaphore.signal()
# semaphore.capacity
# semaphore.available
# Stride.forEach(array, stride, callback)
# Stride.some(array, stride, callback)
# Stride.every(array, stride, callback)
# Stride.reduce(array, stride, callback, initial)
# Stride.set(array, stride, item)
# Stride.transform(array, stride, callback)
#
new URL(url [, parser])
#
new URL(url, baseUrl [, parser])
# url.protocol
# url.slashes
# url.auth
# url.username
# url.password
# url.host
# url.hostname
# url.port
# url.pathname
# url.query
# url.hash
# url.href
# url.origin
# url.toString()
The MIT License
Copyright (C) 2016-Present Shota Matsuda
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.