A
Size: a a a
A
ID
ID
A
A
A
ID
ID
DO
if (selectedOptionsTerrain) {
planets = planets.filter(p => p.terrain.split(', ').includes(selectedOptionsTerrain));
}
const findPlanetsByTerrain = (terrain: string) => filter(pipe(
prop('terrain'),
split(','),
includes(terrain)
));
if (this.state.selectedOptionsPopulation) {
planets = planets.filter(planet => planet.population > this.state.selectedOptionsPopulation)
}
const findPlanetsWherePopulationMoreThan = (population: number) =>
filter((planet: Planet) => planet.population > population);
const filterPlanets = (terrain: string, population: number) => pipe(
when(() => isPresent(terrain), findPlanetsByTerrain(terrain)),
when(() => isPresent(population), findPlanetsWherePopulationMoreThan(population))
);
DO
const { filter, pipe, prop, split, includes, isNil, negate } = require('lodash/fp');
interface Planet {
terrain: string;
population: number;
}
const planets: Planet[] = [{
terrain: 'ter1,ter2,ter3',
population: 500000
}, {
terrain: 'ter3,ter4,ter5',
population: 1200000
}];
const isPresent = negate(isNil);
const when = (conditionFunc: Function, onTrueFunc: Function) => (data: any) =>
conditionFunc(data) ? onTrueFunc(data) : data;
const filterPlanets = (terrain: string, population: number) => pipe(
when(() => isPresent(terrain), findPlanetsByTerrain(terrain)),
when(() => isPresent(population), findPlanetsWherePopulationMoreThan(population))
);
const findPlanetsByTerrain = (terrain: string) => filter(pipe(
prop('terrain'),
split(','),
includes(terrain)
));
const findPlanetsWherePopulationMoreThan = (population: number) =>
filter((planet: Planet) => planet.population > population);
const testFilterPlanets = () => {
console.log('filterPlanets', filterPlanets('ter5', 1000000)(planets));
};
testFilterPlanets();
DO
isPresent
и when
. К сожалению lodash/fp
не включает when()
AV
ID
isPresent
и when
. К сожалению lodash/fp
не включает when()
DO
if (this.state.selectedOptionsTerrain) {
planets = planets.filter(planet => planet.terrain.split(', ').includes(this.state.selectedOptionsTerrain));
}
if (this.state.selectedOptionsPopulation) {
planets = planets.filter(planet => planet.population > this.state.selectedOptionsPopulation)
}
const filterPlanets = (terrain: string, population: number) => pipe(
when(() => isPresent(terrain), findPlanetsByTerrain(terrain)),
when(() => isPresent(population), findPlanetsWherePopulationMoreThan(population))
);
SG
ID
SG
SG
SG
SG