Giter Club home page Giter Club logo

songojs's Introduction

songo.js

Greenkeeper badge Build StatusCoverage StatusDependency

Description

用于配合songo协议的前端js库

参考: https://github.com/suboat/songo

Usage

npm install https://github.com/axetroy/songojs.git --save
const songo = require('songo')
// or
import songo from 'songo';

songo(entity?: Entity):Songo

interface Entity {
  query?: any,
  sort?: string[],
  meta?: {
      limit?:number,
      page?: number,
      skip?: number
  }
}

默认的entity:

describe('test songo', function () {
  it('default entity', function () {
    var so = songo();
    expect(so.meta).to.be.deep.equal({
      limit: 10,
      page: 0,
      skip: 0
    });
    expect(so.sort).to.be.deep.equal([]);
    expect(so.query).to.be.deep.equal({});
  });
})

可以只设置部分值,其他为默认值

describe('test songo partial entity',function() {
  it('只设置部分值,其余值应为默认值', function () {
    var so = songo({
      meta: {limit: 10}
    });
    expect(so.meta).to.be.deep.equal({
      limit: 10,
      page: 0,
      skip: 0
    });
    expect(so.sort).to.be.deep.equal([]);
    expect(so.query).to.be.deep.equal({});
    expect(so + '').to.be.deep.equal('_limit=10&_page=0&_skip=0&_sort=');
  });
});

Songo.meta:Meta

interface Meta {
  limit?: number,
  page?: number,
  skip?: number
}

Songo.sort:string[]

Songo.query:any

可直接操作值,覆盖,删除,在转换url的时候,再次编译。

通过key无限层级,没有嵌套,意味着可以直接在view层使用

意味着这种用法,是安全的: so.query.$and$or$eq$currency = 'USD';

describe('test songo partial entity',function() {
  it('修改meta属性后,重新生成', function () {
    var so = songo({
      meta: {limit: 10},
      sort: ['-created']
    });
    expect(so.meta.limit).to.be.equal(10);
    so.meta.limit = 50;
    expect(so + '').to.be.equal('_limit=50&_page=0&_skip=0&_sort=-created')
  });
});

Songo.toString():string

将对象转换成符合songo协议的url的字符串,用于前后端的数据交互

可以通过**so + ''**来自动触发toString方法

describe('test songo partial entity',function() {
  it('test toString method', function () {
    var so = songo({
      meta: {
        limit: 20,
        page: 2,
        skip: 0
      },
      sort: ['money'],
      query: {
        $eq$fid: '000',
        $gt$money: 100,
        $lg$money: 1000
      }
    });
    expect(so.toString()).to.be.deep.equal(so + '');
    expect(so.toString()).to.be.deep.equal('_limit=20&_page=2&_skip=0&_sort=money&fid=$eq$000&money=$gt$100,$lg$1000')
  })
});

Songo.string:string

等同于Songo.toString()

describe('test songo partial entity',function() {
  it('test string property', function () {
    var so = songo({
      meta: {
        limit: 20,
        page: 2,
        skip: 0
      },
      sort: ['money'],
      query: {
        $eq$fid: '000',
        $gt$money: 100,
        $lg$money: 1000
      }
    });
    expect(so.string).to.be.deep.equal(so + '');
    expect(so.string).to.be.deep.equal(so.toString());
    expect(so.string).to.be.deep.equal('_limit=20&_page=2&_skip=0&_sort=money&fid=$eq$000&money=$gt$100,$lg$1000')
  })
});

Songo.toQuery():string

Songo.string()的别名

Songo.toParams():Params

将对象转换成适合放在url上的object对象,用于前端页面,而非前后端的数据交互

interface Params {
  limit: number,
  page: number,
  skip?: number,
  sort?: string,
  query?: string
}

Songo.fromParams(paramsObject:Params):Songo

将Songo.toParams生成的对象,还原为Songo对象

用于从url读取query,然后还原

interface Params {
  limit: number,
  page: number,
  skip?: number,
  sort?: string,
  query?: string
}
describe('test params parser',function() {
  it('toParams 和 fromParams转码一致', function () {
    var so = songo({
      meta: {
        limit: 20,
        page: 2,
        skip: 0
      },
      sort: ['money'],
      query: {
        $eq$fid: '000',
        $gt$money: 100,
        $lg$money: 1000
      }
    });
    expect(so.toParams()).to.be.deep.equal(so.fromParams(so.toParams()).toParams())
  })  
});

Songo.clearSort():Songo

清除排序sort字段为空数组

Songo.onlySort(sortKey: string):Songo

设置一个唯一的sort排序字段

describe('test only sort method',function() {
  it('only sort', function () {
    var so = songo({
      meta: {limit: 10},
      sort: ['-money']
    });
    expect(so.sort).to.be.deep.equal(['-money']);
    expect(so + '').to.be.deep.equal('_limit=10&_page=0&_skip=0&_sort=-money');

    so.onlySort('created');
    expect(so.sort).to.be.deep.equal(['created']);
    expect(so + '').to.be.deep.equal('_limit=10&_page=0&_skip=0&_sort=created');

    so.onlySort('-level');
    expect(so.sort).to.be.deep.equal(['-level']);
    expect(so + '').to.be.deep.equal('_limit=10&_page=0&_skip=0&_sort=-level');
  });
})

Songo.setSort(sortKey: string):Songo

添加一个sort排序字段,如果已存在的字段,则删除旧字段,把新字段放在数组的第一个

describe('test only sort method',function() {
  it('set sort', function () {
    var so = songo({
      meta: {limit: 10},
      sort: ['-money']
    });
    expect(so.sort).to.be.deep.equal(['-money']);
    expect(so + '').to.be.deep.equal('_limit=10&_page=0&_skip=0&_sort=-money');

    so.setSort('created');

    expect(so.sort).to.be.deep.equal(['created', '-money']);
    expect(so + '').to.be.deep.equal('_limit=10&_page=0&_skip=0&_sort=created,-money');

    so.setSort('money');

    expect(so.sort).to.be.deep.equal(['money', 'created']);
    expect(so + '').to.be.deep.equal('_limit=10&_page=0&_skip=0&_sort=money,created');

  });
})

Songo.unshiftSort(sortKey: string):Songo

目前,效果等同于Songo.setSort()

describe('test only sort method',function() {
  it('unshift sort', function () {
    var so = songo({
      meta: {limit: 10},
      sort: ['-created', 'status']
    });
    expect(so.sort).to.be.deep.equal(['-created', 'status']);
    expect(so + '').to.be.deep.equal('_limit=10&_page=0&_skip=0&_sort=-created,status');
    so.unshiftSort('money');
    expect(so.sort).to.be.deep.equal(['money', '-created', 'status']);
    expect(so.sort.length).to.be.deep.equal(3);
    expect(so + '').to.be.deep.equal('_limit=10&_page=0&_skip=0&_sort=money,-created,status');
  });
})

Songo.removeSort(sortKey:string):Songo

删除某个排序字段

describe('test only sort method',function() {
  it('remove sort', function () {
    var so = songo({
      meta: {limit: 10},
      sort: ['-created', 'status']
    });
    expect(so.sort).to.be.deep.equal(['-created', 'status']);
    expect(so + '').to.be.deep.equal('_limit=10&_page=0&_skip=0&_sort=-created,status');
    so.removeSort('status');
    expect(so.sort).to.be.deep.equal(['-created']);
    expect(so.sort.length).to.be.deep.equal(1);
    expect(so + '').to.be.deep.equal('_limit=10&_page=0&_skip=0&_sort=-created');
  });
})

Songo.pushSort(sortKey:string):Songo

等同于Array.push()

describe('test only sort method',function() {
  it('push sort', function () {
    var so = songo({
      meta: {limit: 10},
      sort: ['-created', 'status']
    });
    expect(so.sort).to.be.deep.equal(['-created', 'status']);
    expect(so + '').to.be.deep.equal('_limit=10&_page=0&_skip=0&_sort=-created,status');
    so.pushSort('level');
    expect(so.sort).to.be.deep.equal(['-created', 'status', 'level']);
    expect(so.sort.length).to.be.deep.equal(3);
    expect(so + '').to.be.deep.equal('_limit=10&_page=0&_skip=0&_sort=-created,status,level');
  });
})

Songo.popSort(sortKey:string):Songo

等同于Array.pop()

describe('test only sort method',function() {
  it('pop sort', function () {
    var so = songo({
      meta: {limit: 10},
      sort: ['-created', 'status']
    });
    expect(so.sort).to.be.deep.equal(['-created', 'status']);
    expect(so + '').to.be.deep.equal('_limit=10&_page=0&_skip=0&_sort=-created,status');
    so.popSort();
    expect(so.sort).to.be.deep.equal(['-created']);
    expect(so.sort.length).to.be.deep.equal(1);
    expect(so + '').to.be.deep.equal('_limit=10&_page=0&_skip=0&_sort=-created');
  });
})

Songo.shiftSort(sortKey:string):Songo

等同于Array.shiftSort()

describe('test only sort method',function() {
  it('shift sort', function () {
    var so = songo({
      meta: {limit: 10},
      sort: ['-created', 'status']
    });
    expect(so.sort).to.be.deep.equal(['-created', 'status']);
    expect(so + '').to.be.deep.equal('_limit=10&_page=0&_skip=0&_sort=-created,status');
    so.shiftSort();
    expect(so.sort).to.be.deep.equal(['status']);
    expect(so.sort.length).to.be.deep.equal(1);
    expect(so + '').to.be.deep.equal('_limit=10&_page=0&_skip=0&_sort=status');
  });
})

Contribution

git clone https://github.com/axetroy/songojs.git
cd songojs
npm install gulp-cli -g
npm install mocha -g
npm install 

npm run build

Test

npm run test
// or
npm run test:watch

test detail:

https://travis-ci.org/axetroy/songojs

License

The MIT License (MIT)

Copyright (c) 2016 axetroy

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.

songojs's People

Contributors

axetroy avatar greenkeeper[bot] avatar

Watchers

James Cloos avatar  avatar  avatar

songojs's Issues

An in-range update of ts-node is breaking the build 🚨

Version 3.0.5 of ts-node just got published.

Branch Build failing 🚨
Dependency ts-node
Current Version 3.0.4
Type devDependency

This version is covered by your current version range and after updating it in your project the build failed.

As ts-node is “only” a devDependency of this project it might not break production or downstream projects, but “only” your build or test tools – preventing new deploys or publishes.

I recommend you give this issue a high priority. I’m sure you can resolve this 💪

Status Details
  • continuous-integration/travis-ci/push The Travis CI build failed Details

Commits

The new version differs by 7 commits.

  • c18331a 3.0.5
  • 5cf97b1 Add --inspect-brk to known flags (#343)
  • 7dfb3e3 Pin node tests at 6.x (#340)
  • 633d537 chore(package): update chai to version 4.0.1 (#337)
  • b751a56 Define exports and improve getTypeInfo help (#332)
  • d018300 Update yn default option
  • cc3bf22 Expose _ bin file for consumers to use

See the full diff

Not sure how things should work exactly?

There is a collection of frequently asked questions and of course you may always ask my humans.


Your Greenkeeper Bot 🌴

An in-range update of ts-loader is breaking the build 🚨

Version 2.0.3 of ts-loader just got published.

Branch Build failing 🚨
Dependency ts-loader
Current Version 2.0.2
Type devDependency

This version is covered by your current version range and after updating it in your project the build failed.

As ts-loader is “only” a devDependency of this project it might not break production or downstream projects, but “only” your build or test tools – preventing new deploys or publishes.

I recommend you give this issue a high priority. I’m sure you can resolve this 💪


Status Details
  • continuous-integration/travis-ci/push The Travis CI build could not complete due to an error Details
Release Notes v2.0.3
Commits

The new version differs by 2 commits .

  • 6c73327 prepare for 2.0.3 release (#510)
  • 5e20c2c Don't include appended TS extension in webpack dependencies. (#497)

See the full diff.

Not sure how things should work exactly?

There is a collection of frequently asked questions and of course you may always ask my humans.


Your Greenkeeper Bot 🌴

An in-range update of ts-node is breaking the build 🚨

Version 2.1.2 of ts-node just got published.

Branch Build failing 🚨
Dependency ts-node
Current Version 2.1.1
Type devDependency

This version is covered by your current version range and after updating it in your project the build failed.

As ts-node is “only” a devDependency of this project it might not break production or downstream projects, but “only” your build or test tools – preventing new deploys or publishes.

I recommend you give this issue a high priority. I’m sure you can resolve this 💪


Status Details
  • continuous-integration/travis-ci/push The Travis CI build could not complete due to an error Details
Release Notes Config Defaults

Fixed

  • Set config defaults after configuration file has been parsed so that they don't override "extended" compiler options (ec00796)
Commits

The new version differs by 3 commits .

  • 4a17778 v2.1.2
  • ec00796 Set default config options after parsing contents
  • b251147 Make tests work under Windows (avoid | in shell)

See the full diff.

Not sure how things should work exactly?

There is a collection of frequently asked questions and of course you may always ask my humans.


Your Greenkeeper Bot 🌴

An in-range update of @types/core-js is breaking the build 🚨

Version 0.9.38 of @types/core-js just got published.

Branch Build failing 🚨
Dependency @types/core-js
Current Version 0.9.37
Type devDependency

This version is covered by your current version range and after updating it in your project the build failed.

As @types/core-js is “only” a devDependency of this project it might not break production or downstream projects, but “only” your build or test tools – preventing new deploys or publishes.

I recommend you give this issue a high priority. I’m sure you can resolve this 💪


Status Details
  • continuous-integration/travis-ci/push The Travis CI build could not complete due to an error Details
Not sure how things should work exactly?

There is a collection of frequently asked questions and of course you may always ask my humans.


Your Greenkeeper Bot 🌴

An in-range update of babel-preset-es2015 is breaking the build 🚨

Version 6.24.1 of babel-preset-es2015 just got published.

Branch Build failing 🚨
Dependency babel-preset-es2015
Current Version 6.24.0
Type devDependency

This version is covered by your current version range and after updating it in your project the build failed.

As babel-preset-es2015 is “only” a devDependency of this project it might not break production or downstream projects, but “only” your build or test tools – preventing new deploys or publishes.

I recommend you give this issue a high priority. I’m sure you can resolve this 💪


Status Details
  • continuous-integration/travis-ci/push The Travis CI build could not complete due to an error Details
Not sure how things should work exactly?

There is a collection of frequently asked questions and of course you may always ask my humans.


Your Greenkeeper Bot 🌴

An in-range update of @types/mocha is breaking the build 🚨

Version 2.2.41 of @types/mocha just got published.

Branch Build failing 🚨
Dependency @types/mocha
Current Version 2.2.40
Type devDependency

This version is covered by your current version range and after updating it in your project the build failed.

As @types/mocha is “only” a devDependency of this project it might not break production or downstream projects, but “only” your build or test tools – preventing new deploys or publishes.

I recommend you give this issue a high priority. I’m sure you can resolve this 💪

Status Details - ❌ **continuous-integration/travis-ci/push** The Travis CI build could not complete due to an error [Details](https://travis-ci.org/axetroy/songojs/builds/226790441?utm_source=github_status&utm_medium=notification)

Not sure how things should work exactly?

There is a collection of frequently asked questions and of course you may always ask my humans.


Your Greenkeeper Bot 🌴

An in-range update of @types/node is breaking the build 🚨

Version 7.0.10 of @types/node just got published.

Branch Build failing 🚨
Dependency @types/node
Current Version 7.0.9
Type devDependency

This version is covered by your current version range and after updating it in your project the build failed.

As @types/node is “only” a devDependency of this project it might not break production or downstream projects, but “only” your build or test tools – preventing new deploys or publishes.

I recommend you give this issue a high priority. I’m sure you can resolve this 💪


Status Details
  • continuous-integration/travis-ci/push The Travis CI build could not complete due to an error Details
Not sure how things should work exactly?

There is a collection of frequently asked questions and of course you may always ask my humans.


Your Greenkeeper Bot 🌴

An in-range update of webpack is breaking the build 🚨

Version 2.3.2 of webpack just got published.

Branch Build failing 🚨
Dependency webpack
Current Version 2.3.1
Type devDependency

This version is covered by your current version range and after updating it in your project the build failed.

As webpack is “only” a devDependency of this project it might not break production or downstream projects, but “only” your build or test tools – preventing new deploys or publishes.

I recommend you give this issue a high priority. I’m sure you can resolve this 💪


Status Details
  • continuous-integration/travis-ci/push The Travis CI build could not complete due to an error Details
Release Notes v2.3.2

Bugfixes:

  • Fix performance issue with cheap-source-maps
  • Fix a line offset issue with cheap-source-maps
  • Allow lowercased drive letters as absolute paths (not recommended)
  • Improve some error reporting
Commits

The new version differs by 9 commits .

  • 6b209b1 2.3.2
  • 88f2833 Merge pull request #4567 from webpack/bugfix/cheap-source-map
  • 3fca75e Merge pull request #4565 from webpack/dx/error-description-output-path
  • c226077 update webpack-sources dependency to fix a bug
  • 2fe82af Suggest course of action
  • 56d09ab Merge pull request #4533 from michael-ciniawsky/schema
  • b5451ef Merge pull request #4564 from webpack/bugfix/cheap-source-map
  • 22c771f update webpack-sources dependency to avoid performance issue
  • c390700 fix(schema/absolutePaths): make absolute paths case insensitive

See the full diff.

Not sure how things should work exactly?

There is a collection of frequently asked questions and of course you may always ask my humans.


Your Greenkeeper Bot 🌴

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.