npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

strictobject

v0.1.1

Published

Create auto-validating object

Downloads

2

Readme

StrictObject.js

自動でvalidateしてくれるStrictなObjectを作ります。

セットしようとした値が正しいかどうかチェックし、不正な値であれば無視します。

要件

  • Object.definePropertyが実装されている環境

使い方

newする際、第一引数に定義を渡します

new StrictObject({/* DEFINITION */});

例えば、以下のように定義します

var user = new StrictObject({
  name: { type: String, default: 'anonymous' },
  mail: { type: /^\w+@[a-zA-Z_]+?\.[a-zA-Z]{2,3}$/ },
  login: { type: Number, default: 0 },
  last: { type: Date, default: Date.now() },
  type: { type: ['open', 'public', 'private'], default: 'open' }
});

値をセットします

user.name = 'geta6'

値を参照します

console.log(user.name); // 'geta6'

続けて、不正な値をセットします

user.name = 12345

値を参照します

console.log(user.name); // 'geta6'

type: Stringとあるので、Number型の12345は正しく無視されます

DEFINITION

key | default | val -----------|----------|----- type | null | 正規表現、候補を列記した配列、型の名前を受け付けます(StringNumberArraySelf-Definition) default | null | デフォルト値を設定します writable | true | falseにすると、デフォルト値セット以降の変更を受け付けなくなります enumerable | true | falseにすると、StrictObject.get()した際に、その値を非表示にします validate | function | 値を第一引数・キーを第二引数に取り、返り値にBooleanを期待します、falseなら必ず無視します

type -> validateの順番でチェックします

Method

StrictObject.get([key])

keyに相当する値を返します。 無引数で実行すると、全ての値を含むオブジェクトを返します。 enumerablefalseに設定されていると表示されません。

StrictObject.set(key, [val])

keyは必須です。 keyvalをセットします。 val無しで実行するとデフォルト値か、デフォルト値がなければnullをセットします。

StrictObject.propertyName = 'hoge'のように、通常通りの操作でアクセス可能です。

Example

so = new StrictObject
  style:
    type: ['view', 'json', 'feed']
    default: 'view'
    writable: no
  query:
    type: String
    validate: (val) ->
      return /^[A-Za-z0-9\s]*$/.test val
  start:
    type: Number
    default: 0
  limit:
    type: Number
    default: 50
  order:
    type: ['name', 'date', 'view', 'star', 'cmnt', 'rate']
    default: 'date'
  mtype:
    type: String
  mtime:
    type: /[0-9]+(hour|day|week|month|year)/
    default: null
  fonly:
    type: Boolean
    default: yes
  hides:
    type: null
    enumerable: no
    default: 'free hidden form'

so.style = 'json'           # Valid but NOT writable
so.style = 'hoge'           # Invalid
so.query = 234              # Invalid
so.query = '234'            # Valid
so.query = './../../../etc' # Invalid (validate function)
so.mtime = '1hour'          # Valid
so.mtime = '1minutes'       # Invalid

console.log so.get()
console.log '>>', so.hides

###
{ style: 'view',
  query: '234',
  start: 0,
  limit: 50,
  order: 'date',
  mtype: null,
  mtime: '1hour',
  fonly: true }
>> free hidden form
###