Giter VIP home page Giter VIP logo

snippet's Introduction

Snippet

snippet codes

List

JAVA

  1. Specification reference with WIKI
  • usage
// JUNIT_TEST
@Test
test() {
  TestAggregateRoot mock = new TestAggregateRoot();
  mock.setFlag("a");
  mock.setFlag("b");
  mock.setFlag("c");
  Specification<TestAggregateRoot> hasA = Specification.of(a -> a.hasFlag("a"));
  Specification<TestAggregateRoot> hasB = Specification.of(a -> a.hasFlag("b"));
  Specification<TestAggregateRoot> hasC = Specification.of(a -> a.hasFlag("c"));
  Specification<TestAggregateRoot> hasD = Specification.of(a -> a.hasFlag("d"));

  Assertions.assertTrue(hasA.and(hasB).and(hasC).isSatisfiedBy(mock), "Test operator and");
  Assertions.assertFalse(hasA.and(hasB).and(hasC).and(hasD).isSatisfiedBy(mock),
    "Test operator and");
  Assertions.assertTrue(hasA.andNot(hasD).isSatisfiedBy(mock),
    "Test operator andNot");
  Assertions.assertTrue(hasD.not().isSatisfiedBy(mock), "Test operator not ");
  Assertions.assertTrue(hasA.or(hasD).isSatisfiedBy(mock), "Test operator or");
  Assertions.assertTrue(hasA.and(hasD).or(hasB).isSatisfiedBy(mock), "Test operators and, or");
  Assertions.assertTrue(hasA.and(hasD).or(hasB).isSatisfiedBy(mock), "Test operators and, or");
}

JS

  1. MatchBuilder
const CASE_A = 'caseA'
const CASE_B = 'caseB'
const CASE_C = 'caseC'

// when not use MatchBuilder
const someSwitch = (key, ...args) => {
  switch(key) {
    case CASE_A:
      return `it's ${key} and args are ${...args}`
    case CASE_B:
      return `it's ${key} and args are ${...args}`
    case CASE_C:
      return `it's ${key} and args are ${...args}`
    default:
      return `it's default`;
  }
}

someSwitch(CASE_A, 1, 2, 3); // each case logic has low reusability

// when use MatchBuilder
import MatchBuilder from '{your}/{local}/{path}/util.ts'

const matcher = new MatchBuilder()
  .route(CASE_A, (key, ...args) => `it's ${key} and args are ${...args}`)
  .route(CASE_B, (key, ...args) => `it's ${key} and args are ${...args}`)
  .route(CASE_C, (key, ...args) => `it's ${key} and args are ${...args}`)
  .default(() => `it's default`)
  .build();

console.log(matcher(CASE_A, 1, 2, 3)); // `it's caseA and args are 1,2,3`
console.log(matcher(CASE_B, 'a', 'b', 'c')); // `it's caseB and args are a,b,c`
console.log(matcher(CASE_C)); // `it's caseC and args are a,b,c`
console.log(matcher('any key')); // `it's default`

snippet's People

Contributors

moonchanyong avatar

Watchers

 avatar

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.