0.8.1 • Published 3 years ago

@takram/planck-core v0.8.1

Weekly downloads
2
License
MIT
Repository
github
Last release
3 years ago

Planck Core

A collection of classes and functions that are used anywhere in Planck framework, while is not precisely it’s core.

License npm version JavaScript Style Guide Build Status codecov Sauce Test Status

Sauce Test Status

Getting Started

Installing

npm install @takram/planck-core

Usage

Namespacing

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.

Example

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
  }
}

Property and Function Call Aggregation

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.

Example

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']

Environment Detection

Global distinguishes browsers, workers and node processes by isBrowser, isWorker and isNode respectively, and provides unified way to access the global scopes via scope.

Example

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()

Promise-based Semaphore

The example below limits simultaneous requests by the function load to 10.

Example

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))

API Reference

Aggregate

# Aggregate(target1 [, target2 , ...])

# aggregateproperty

# aggregateproperty = value

# aggregate([arg1 [, arg2 , ...]])

Array

# Array.min(array, transform)

# Array.max(array, transform)

AssertionError

# new AssertionError(message)

FilePath

# 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

# Global.isBrowser

# Global.isWorker

# Global.isNode

# Global.scope

Hash

# Hash(object)

ImplementationError

# new ImplementationError(message)

Math

# 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

# Namespace(name)

# namespace(object , init)

Request

# 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()

Semaphore

# new Semaphore(capacity)

# semaphore.wait(callback)

# semaphore.signal()

# semaphore.capacity

# semaphore.available

Stride

# 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)

URL

# 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()

License

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.

0.8.1

3 years ago

0.7.0

4 years ago

0.6.0

4 years ago

0.5.1

4 years ago

0.5.0

4 years ago

0.4.3

4 years ago

0.4.2

4 years ago

0.4.1

4 years ago

0.4.0

4 years ago

0.3.0

4 years ago

0.2.6

4 years ago

0.2.5

4 years ago

0.2.4

4 years ago

0.2.3

4 years ago

0.2.2

4 years ago

0.2.1

4 years ago

0.2.0

4 years ago

0.1.22

4 years ago

0.1.21

5 years ago

0.1.20

5 years ago

0.1.19

5 years ago

0.1.18

5 years ago

0.1.17

5 years ago

0.1.16

5 years ago

0.1.15

5 years ago

0.1.14

5 years ago

0.1.13

5 years ago

0.1.12

5 years ago

0.1.11

5 years ago

0.1.10

5 years ago

0.1.9

5 years ago

0.1.8

5 years ago