Skip to content

Commit 4cd3033

Browse files
committed
删除react的代码。增加ramda库的示例代码
1 parent 2b6eaed commit 4cd3033

File tree

6 files changed

+67
-20
lines changed

6 files changed

+67
-20
lines changed

.babelrc

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
11
{
22
"plugins": ["transform-react-jsx"],
3-
"presets": ["es2015", "react"]
3+
"presets": ["es2015", "react", "stage-3"]
44
}

basicSyntaxSample/ramda.js

+64-8
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
* @Author: Ping Qixing
33
* @Date: 2017-07-03 08:44:15
44
* @Last Modified by: Ping Qixing
5-
* @Last Modified time: 2017-07-04 14:33:12
5+
* @Last Modified time: 2017-07-05 16:49:06
66
* @Description
77
*/
88
import R from 'ramda';
@@ -107,7 +107,7 @@ let arr2 = arr.reduce((newArr, x) => {
107107
return newArr;
108108
}, []);
109109

110-
console.log(arr2);
110+
// console.log(arr2);
111111

112112
// 而transduce就是执行`变形`和`累积`两个运算,让代码具备更高的复用性。
113113

@@ -121,20 +121,76 @@ let append = (newArr, x) => {
121121
};
122122

123123
let r10 = R.transduce(R.map(plusOne), append, [], arr);
124-
console.log(r10);
124+
// console.log(r10);
125125

126126
// or
127127
let r11 = R.into([], R.map(R.add(1)), arr);
128-
console.log(r11);
128+
// console.log(r11);
129129

130130
let basket = [
131-
{item: 'apples', per: 0.95, count: 3, cost: 2.85},
132-
{item: 'peaches', per: 0.80, count: 2, cost: 1.60},
133-
{item: 'plums', per: 0.55, count: 4, cost: 2.20}
131+
{item: 'apples', per: 0.95, count: 3, cost: 2.85, time: 1499231281164},
132+
{item: 'peaches', per: 0.80, count: 2, cost: 1.60, time: 1499231281264},
133+
{item: 'plums', per: 0.55, count: 4, cost: 2.20, time: 1499231281364}
134134
];
135135

136136
let add = (a, b) => a + b;
137137
let priceSum = R.reduce(add, 0);
138138
// console.log(R.pluck('cost')(basket));
139139
let totalPrice = R.compose(priceSum, R.pluck('cost'));
140-
console.log(totalPrice(basket));
140+
totalPrice(basket);
141+
142+
function print (items) {
143+
R.map(i => {
144+
console.log(`${i.item} ${i.cost} ${i.time}`);
145+
})(items);
146+
}
147+
148+
function modify (item) {
149+
if (item.time instanceof Date) {
150+
return {...item};
151+
} else {
152+
let t = new Date(item.time);
153+
return {
154+
...item,
155+
time: t
156+
}
157+
}
158+
}
159+
160+
R.pipe(
161+
R.map(modify),
162+
print
163+
)(basket);
164+
165+
let CARS = [
166+
{name: 'Ferrari FF', horsepower: 660, dollar_value: 700000, in_stock: true},
167+
{name: 'Spyker C12 Zagato', horsepower: 650, dollar_value: 648000, in_stock: false},
168+
{name: 'Jaguar XKR-S', horsepower: 550, dollar_value: 132000, in_stock: false},
169+
{name: 'Audi R8', horsepower: 525, dollar_value: 114200, in_stock: false},
170+
{name: 'Aston Martin One-77', horsepower: 750, dollar_value: 1850000, in_stock: true},
171+
{name: 'Pagani Huayra', horsepower: 700, dollar_value: 1300000, in_stock: false}
172+
];
173+
174+
let debugTrace = R.curry((tag, v) => {
175+
console.log(tag, v);
176+
return v;
177+
})
178+
179+
let isLastInStock = R.compose(R.prop('in_stock'), R.last);
180+
// console.log(isLastInStock(CARS));
181+
182+
let nameOfFirstCar = R.compose(R.prop('name'), R.head);
183+
// console.log(nameOfFirstCar(CARS));
184+
185+
let _average = function (xs) { return R.reduce(R.add, 0, xs) / xs.length; };
186+
let averageDollarValue = R.compose(_average, R.pluck('dollar_value'));
187+
// console.log(averageDollarValue(CARS));
188+
189+
let _underscore = R.replace(/\W+/g, '_'); // <-- 无须改动,并在 sanitizeNames 中使用它
190+
191+
let sanitizeNames = R.compose(R.map(_underscore), R.map(R.toLower));
192+
// console.log(sanitizeNames(['Hello World', 'What You Want?']));
193+
194+
let sortByHorsepower = R.sortBy(R.prop('horsepower'));
195+
let fasterCar = R.compose(R.last, sortByHorsepower);
196+
console.log(`${fasterCar(CARS).name} is the fastest`);

package.json

+1
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222
},
2323
"homepage": "https://github.com/navono/JSPracticeSample#readme",
2424
"devDependencies": {
25+
"babel-preset-stage-3": "^6.24.1",
2526
"gulp": "^3.9.1",
2627
"gulp-uglify": "^3.0.0",
2728
"html-webpack-plugin": "^2.29.0",

reactSample/reactDemo.html

-3
This file was deleted.

reactSample/reactDemo.js

-7
This file was deleted.

resolve-dir/index.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ var path = require('path');
1111
var expand = require('expand-tilde');
1212
var gm = require('global-modules');
1313

14-
module.exports = function resolveDir(dir) {
14+
module.exports = function resolveDir (dir) {
1515
if (dir.charAt(0) === '~') {
1616
dir = expand(dir);
1717
}

0 commit comments

Comments
 (0)