アプリ関連ニュース

Lodash Js Part 2(Chaining and has method)

Today I will talk about chaining functions and has method.

Chaining

Without Chaining

var data1 = [10, 15, 20, 25, 15];

var data2 = _.filter(data1, function(item){ return item % 10 === 5 });
// data2 now contains [15, 25, 15]

var data3 = _.uniq(data2);
// data3 now contains [15, 25]

var data4 = _.map(data3, function(item){ return item + 1 });
// data4 now contains [16, 26]

With Chaining

var data1 = [10, 15, 20, 25, 15];

var data4 = _(data1)
    .filter(function(item){ return item % 10 === 5 })
    .uniq()
    .map(function(item){ return item + 1 })
    .value();
// data4 now contains [16, 26]
  • The chaining is more efficient.
  • no intermediate results are created.

Slight Changes for Explicit Chain & Implicit Chain

Explicit chaining

Still need to unwrap the result with .value().

var data = [1, 5, 10, 15, 5, 10, 1, 20];

var unqSum = _.chain(data)
   .uniq()
   .sum()       
   .value();   
result is 51

Implicit chaining

The “unwrapping” of the single value is implied. (there is no need to call .value().)

var data = [1, 5, 10, 15, 5, 10, 1, 20];

var unqSum = _(data)
    .uniq()
    .sum();  
    
    //result is 51

has

  • Determine if an object has (or contains) a key.
var obj = {
  data1: 2,
  data2: 3,
  data3: {
    data3_1:40,
    data3_2:{
      data3_2_1:500
    }
  }
};

var res1 = _.has(obj, "data1");          // true
var res2 = _.has(obj, "data1.data2");        // false
var res3 = _.has(obj, "data3");          // true
var res4 = _.has(obj, "data3.data3_2");       // true
var res5 = _.has(obj, "data3.res3_2_1");      // false
var res6 = _.has(obj, "data3.data3_1.data3_2_1");   // false
var res7 = _.has(obj, "data3.data3_2.data3_2_1");   // true
  • Arrays can be used to split up parts.
var res8 = _.has(obj, ["data1", "data2"]);   // false, same as res2
var res9 = _.has(obj, ["data3", "data3_2"]);  // true, same as res4

By Yuuma



[php] printf sprintf vprintf vsprintf の違いについて(その1)

PHPでフォーマットした文字列の出力をおこなう際に使用する関数、
printf sprintf vprintf vsprintf の使い分けについて記載します。

続きを読む

Introduction to Loadash Js and its `identity` property (part 1)

What is Lodash

Lodash is a JavaScript library which provides utility functions for common programming tasks, delivering consistency, modularity and performance etc and it provides simple to use methods that solve common problems.

Why Lodash

  • Provides consistent cross browser support.
  • Able to use both for frontend & backend.
  • Lodash can follow DRY principle.
  • Performance and optimization for speed is better rather than native and more readable.
  • Declarative type.
  • Have good amount of contributors and active in their respositories.
  • Support every browser basically.

How

  • Can use locally or globally
  • Can also go through with npm
  • For backend use, have to require lodash in code.

I will introduce with a property called identity using in lodash js

Identity

var res = _.identity('hlaing', 'tin');
// res is hlaing

Method returns the first argument

_.identity in documentation of _.findKey and _.findLastKey

var data = {
    topic: "lodash",
    desc: "js library",
    awesome: "chaining"
};

var keyRes = _.findKey(p);        // returns "topic"
var lastKeyRes = _.findLastKey(p);    // returns "awesome"

// same with above 
var keyRes = _.findKey(p, _.identity);
var lastKeyRes = _.findLastKey(p, _.identity);

// which again means the same scenario:
var keyRes = _.findKey(p, function(x) { return x; }
var lastKeyRes = _.findLastKey(p, function(x) { return x; }

I introduced the identity property this week and will introduce the rest functionalities in coming weeks.

By Yuuma



Windows 10 上でbashを実行しよう

Linux用にライブラリをビルドしたかったので、
Windows10に追加された Windows Subsystem for Linux(以下 WSL) 機能を
使ってbashスクリプトを実行できる環境を作ります。

続きを読む

Session & Cookie

Session Information Resides on the Web Server

A session is server-side information intended to exist only throughout the visitor’s interaction with the website. Only a unique identifier is stored on the client side. This token is passed to the web server when the visitor’s browser requests your HTTP address. That token matches your website with the visitor’s information while the user is at your site. When the user closes the website, the session ends, and your website loses access to the information. If you don’t need any permanent data, sessions are usually the way to go. They are a little easier to use, and they can be as large as needed, in comparison with cookies, which are relatively small.

Sessions cannot be disabled or edited by the visitor.  

So, if you have a site requiring a login, that information is better served as a cookie, or the user would be forced to log in every time he visits. If you prefer tighter security and the ability to control the data and when it expires, sessions work best.

You can, of course, get the best of both worlds. When you know what each does, you can use a combination of cookies and sessions to make your site work exactly the way you want it to work.

Session usage examples

//session global variable
$_SESSION

//session start
session_start(); 

//storing session
$_SESSION["username"] = "admin";
$_SESSION["name"] = "yuuma";
$_SESSION["dept"] = "web";

//retrieving session
echo 'Welcome:' . $_SESSION["username"] . '<br>';  
echo 'Your name is ' . $_SESSION["name"] . '<br>'; 
echo 'Your department is ' . $_SESSION["dept"];
//the output will be look like this.
Welcome:admin
Your name is yuuma
Your department is web

// remove all session variables
session_unset(); 

// destroy the session 
session_destroy(); 

A Cookie Resides on the User’s Computer

Your website can be set to place a cookie on a user’s computer. That cookie maintains information in the user’s machine until the information is deleted by the user. A person may have a username and password to your website. That information can be saved as a cookie on the visitor’s computer, so there is no need for him to log in to your website on each visit. Common uses for cookies include authentication, storage of site preferences, and shopping cart items. Although you can store almost any text in a browser cookie, a user can block cookies or delete them at any time. If, for example, your website’s shopping cart utilizes cookies, shoppers who block cookies in their browsers can’t shop at your website.

Cookies can be disabled or edited by the visitor. Do not use cookies to store sensitive data.

Cookie usage examples

//setting cookie
setcookie(name, value, expire, path, domain, security,httponly);

Name– The name of the cookie. It’s mandatory.The server will use when retrieving its value from the $_COOKIE array variable.

Value –The setcookie() function set the value of the name variable and It contains that you actually want to store. It’s also mandatory.

Expiry- This is a limit to access a cookie. The time may be 1 day ,1 month or more. If you set the time 1 month then you can access the cookie during the 1-month duration. After one month you can not access the cookie.

Path- Path specifies the directories from which the cookie is valid .

Domain – This can be used to specify the domain name in very large domains and must contain at least two periods to be valid. All cookies are only valid for the host and domain which created them.

Security- It set to be 1 refers that the cookie should only be sent by secure transmission using HTTPS and else set to 0 which means cookie can be sent by regular HTTP.

httponly If it is set to true, then only client side scripting languages .for example – JavaScript cannot access them. Set the cookie username, mobile, and email. The cookie will expire after one hour in the below example.

setcookie("username", "john", time()+3600, "/","", 0);
setcookie("email", "john@example.com", time()+3600, "/", "", 0);
//retrieving cookie
$_COOKIE['cookiename']

//removing cookie
//To delete a cookie you need to use setcookie() function as well as with its name. You can destroy cookies before the expiry time. You need to define your given expiry time. Let's have a look. The PHP code help destroy the cookies before the expiry time.

setcookie("admin", "", time() - 3600,'/');

By Yuuma



アプリ関連ニュース

お問い合わせはこちら

お問い合わせ・ご相談はお電話、またはお問い合わせフォームよりお受け付けいたしております。

tel. 06-6454-8833(平日 10:00~17:00)

お問い合わせフォーム