Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

helpful-decorators

Package Overview
Dependencies
Maintainers
1
Versions
19
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

helpful-decorators - npm Package Compare versions

Comparing version 2.0.6 to 2.1.0

dist-src/sortby.js

59

dist-node/index.js

@@ -144,3 +144,62 @@ 'use strict';

const sortFunc = (a, b) => {
if (a < b) {
return -1;
} else if (a > b) {
return 1;
}
return 0;
};
/**
* Sort array by a specific element property, its value type must be one of string, number and date
* @param {string | undefined} sortByProperty specify a property from each element that sorting will be based on, undefined means sorty by element itself
* @param {
isDescending: boolean;
sortByPropertyType: string;
} options
* @returns script version
*/
function SortBy(sortByProperty, options = {
isDescending: true,
type: 'string'
}) {
const cachedValueKey = Symbol();
return function (target, propertyKey, descriptor) {
Object.defineProperty(target, propertyKey, {
set: function set(arr) {
if (!arr || !Array.isArray(arr)) {
throw `Value of property ${propertyKey} is not a valid array!`;
} // Perform sorting logic
const isDateType = options.type === 'date';
if (sortByProperty) {
this[cachedValueKey] = arr.sort(function (a, b) {
const aValue = isDateType ? new Date(a[sortByProperty]) : a[sortByProperty];
const bValue = isDateType ? new Date(b[sortByProperty]) : b[sortByProperty];
const sortResult = sortFunc(aValue, bValue);
return options.isDescending ? sortResult * -1 : sortResult;
});
} else {
this[cachedValueKey] = arr.sort(function (a, b) {
const aValue = isDateType ? new Date(a) : a;
const bValue = isDateType ? new Date(b) : b;
const sortResult = sortFunc(aValue, bValue);
return options.isDescending ? sortResult * -1 : sortResult;
});
}
},
get: function get() {
return this[cachedValueKey];
}
});
};
}
exports.Mixin = Mixin;
exports.SortBy = SortBy;
exports.bind = bind;

@@ -147,0 +206,0 @@ exports.debounce = debounce;

3

dist-src/index.js

@@ -8,2 +8,3 @@ export { measure } from "./measure.js";

export { memo } from "./memoize.js";
export { bind } from "./bind.js";
export { bind } from "./bind.js";
export { SortBy } from "./sortby.js";

@@ -9,1 +9,2 @@ export { measure } from './measure';

export { bind } from './bind';
export { SortBy } from './sortby';

@@ -154,2 +154,61 @@ function measure(target, propertyKey, descriptor) {

export { Mixin, bind, debounce, delay, measure, memo, once, throttle };
const sortFunc = (a, b) => {
if (a < b) {
return -1;
} else if (a > b) {
return 1;
}
return 0;
};
/**
* Sort array by a specific element property, its value type must be one of string, number and date
* @param {string | undefined} sortByProperty specify a property from each element that sorting will be based on, undefined means sorty by element itself
* @param {
isDescending: boolean;
sortByPropertyType: string;
} options
* @returns script version
*/
function SortBy(sortByProperty) {
let options = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : {
isDescending: true,
type: 'string'
};
const cachedValueKey = Symbol();
return function (target, propertyKey, descriptor) {
Object.defineProperty(target, propertyKey, {
set: function set(arr) {
if (!arr || !Array.isArray(arr)) {
throw "Value of property ".concat(propertyKey, " is not a valid array!");
} // Perform sorting logic
const isDateType = options.type === 'date';
if (sortByProperty) {
this[cachedValueKey] = arr.sort(function (a, b) {
const aValue = isDateType ? new Date(a[sortByProperty]) : a[sortByProperty];
const bValue = isDateType ? new Date(b[sortByProperty]) : b[sortByProperty];
const sortResult = sortFunc(aValue, bValue);
return options.isDescending ? sortResult * -1 : sortResult;
});
} else {
this[cachedValueKey] = arr.sort(function (a, b) {
const aValue = isDateType ? new Date(a) : a;
const bValue = isDateType ? new Date(b) : b;
const sortResult = sortFunc(aValue, bValue);
return options.isDescending ? sortResult * -1 : sortResult;
});
}
},
get: function get() {
return this[cachedValueKey];
}
});
};
}
export { Mixin, SortBy, bind, debounce, delay, measure, memo, once, throttle };
{
"name": "helpful-decorators",
"description": "Helpful decorators for typescript projects",
"version": "2.0.6",
"version": "2.1.0",
"license": "MIT",

@@ -6,0 +6,0 @@ "esnext": "dist-src/index.js",

@@ -121,2 +121,27 @@ [![npm](https://img.shields.io/npm/dt/helpful-decorators.svg)]()

`SortBy` - sort an array by a specific property in individual elements or non-object items (By default, it sorts by `type === 'string'` and `isDescending === true`)
```js
import { SortBy } from 'helpful-decorators';
class Test {
@SortBy('name', {
isDescending: false,
type: 'string'
})
names = [ { name: 'b' }, { name: 'a' }, { name: 'c' } ];
@SortBy('', {
isDescending: true,
type: 'date'
})
dates = [ '2020-06-17', '2020-06-16', '2020-06-20', '2020-06-10' ];
@SortBy('', {
isDescending: false,
type: 'number'
})
numbers = [ 6, 3, 4, 1 ];
}
```

@@ -123,0 +148,0 @@ License

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc