Git Product home page Git Product logo

fashion-model's People

Contributors

austinkelleher avatar philidem avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar

Forkers

austinkelleher

fashion-model's Issues

Allow individual properties to implement a clean function

It's useful to be able to clean different properties in different ways. For example, if you want to send sensitive data to a client only when a specific condition is met. Here's an example test case:

test('should allow clean function on individual properties', function (t) {
  const Person = Model.extend({
    properties: {
      name: String,
      ssn: {
        type: String,
        clean: function (val, options) {
          t.is(val, 'abc123');
          if (options.showSensitive) return val;
        }
      }
    }
  })

  const person = Person.wrap({
    name: 'John',
    ssn: 'abc123'
  });

  t.deepEqual(person.clean(), { name: 'John' });
  t.deepEqual(person.clean({ showSensitive: true }), {
    name: 'John',
    ssn: 'abc123'
  });
});

Incorrect parsing of dates

Dates with long millisecond precision are being parsed incorrectly.

Test Code

const Model = require("fashion-model/Model");

const Document = Model.extend({
  properties: {
    dateCreated: Date,
  },
});

const input = "2022-01-10T21:15:41.573740635Z";

const document = new Document();
document.setDateCreated(input);
console.log(document.stringify());

Actual Output

{"dateCreated":"2022-01-17T12:38:01.635Z"}

Expected Output

{"dateCreated":"2022-01-10T21:15:41.573Z"}

Primitive Map Type

I think that having a Map primitive would be useful. Example:

const Map = require('fashion-model/Map')
const Model = require('fashion-model/Model')

const Car = Model.extend({
  properties: {
    id: String,
    name: String
  }
})

const CarMap = Map.create({
  key: String,
  value: Car,
  // Only required if the "value" is a Model and if you want to 
  // pass an array instead of an object upon "wrap"/"new"
  keyProperty: 'id',
  prototype: {
    getCarById (id) {
      return this.get(id)
    },
   deleteCarById (id) {
     return this.delete(id)
   }
  }
})

const Person = Model.extend({
  properties: {
    name: String,
    cars: CarMap
  }
})

const person = Person.wrap({
  name: 'John',
  cars: [new Car({ id: 'abc123', name: 'Tesla' })]
  // Also passing a standard key/value pair object is supported:
  //
  // cars: { 'abc123': new Car(...), ... }
})

console.log(person.getCars()) // { 'abc123': { id: 'abc123', name: 'Tesla' } }
console.log(person.getCars().getCarById('abc123')) // { id: 'abc123', name: 'Tesla' }

Thoughts @philidem?

Enum value is not properly cleaned

Example:

var AddressType = Enum.create({
    values: {
        'home': {
            title: 'Home'
        },

        'work': {
            title: 'Work'
        }
    }
});

var Address = Model.extend({
    properties: {
        city: String,
        state: String,
        type: AddressType
    }
});

var address = new Address({
    city: 'Durham',
    state: 'NC',
    type: AddressType.WORK
});

// This expectation is currently failing
expect(Model.clean(address)).to.deep.equal({
    city: 'Durham',
    state: 'NC',
    type: 'work'
});

Confusing type coercion when setting a boolean property

const Model = require('fashion-model/Model');

const example = {
    properties:{
        a: Boolean
    }
}

const Address = Model.extend(example);

const address = new Address();

console.log(address.getA());
address.setA("hello");
console.log(address.getA());

If you run the above code the output is as follows:

undefined
false

when the a variable in address is set to the string primitive "hello" it is coerced into the boolean value false. I think a more appropriate response would be to error when given an invalid type or leave the value undefined.

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.